Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require 'config.php';
- require 'Slim/Slim.php';
- use PHPMailerPHPMailerPHPMailer;
- use PHPMailerPHPMailerException;
- require 'vendor/autoload.php';
- SlimSlim::registerAutoloader();
- $app = new SlimSlim();
- $app->post('/login','login'); /* User login */
- $app->get('sendMail','sendMail'); /* send Email */
- $app->run();
- /************************* USER LOGIN *************************************/
- /* ### User login ### */
- function login() {
- $request = SlimSlim::getInstance()->request();
- $data = json_decode($request->getBody());
- try {
- $db = getDB();
- $userData ='';
- $sql = "SELECT user_id, name, email, username FROM users WHERE (username=:username or email=:username) and password=:password ";
- $stmt = $db->prepare($sql);
- $stmt->bindParam("username", $data->username, PDO::PARAM_STR);
- $password=hash('sha256',$data->password);
- $stmt->bindParam("password", $password, PDO::PARAM_STR);
- $stmt->execute();
- $mainCount=$stmt->rowCount();
- $userData = $stmt->fetch(PDO::FETCH_OBJ);
- if(!empty($userData))
- {
- $user_id=$userData->user_id;
- $userData->token = apiToken($user_id);
- }
- $db = null;
- if($userData){
- $userData = json_encode($userData);
- echo '{"userData": ' .$userData . '}';
- } else {
- echo '{"error":{"text":"Bad request wrong username and password"}}';
- }
- }
- catch(PDOException $e) {
- echo '{"error":{"text":'. $e->getMessage() .'}}';
- }
- }
- /* ### send email test ### */
- function sendMail()
- {
- // below credential is my actual mailtrap credential
- $mail = new PHPMailer(true);
- try {
- //Server settings
- $mail->SMTPDebug = 2; // Enable verbose debug output
- $mail->isSMTP(); // Set mailer to use SMTP
- $mail->Host = 'smtp.mailtrap.io'; // Specify main and backup SMTP servers
- $mail->SMTPAuth = true; // Enable SMTP authentication
- $mail->Username = '1fd562fc56d940'; // SMTP username
- $mail->Password = 'af5ec48a984df1'; // SMTP password
- $mail->SMTPSecure = 'PLAIN'; // Enable TLS encryption, `ssl` also accepted
- $mail->Port = 2525; // TCP port to connect to
- //Recipients
- $mail->setFrom('from@example.com', 'Mailer');
- $mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
- // $mail->addAddress('ellen@example.com'); // Name is optional
- // $mail->addReplyTo('info@example.com', 'Information');
- // $mail->addCC('cc@example.com');
- // $mail->addBCC('bcc@example.com');
- //Attachments
- // $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
- // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
- //Content
- $mail->isHTML(true); // Set email format to HTML
- $mail->Subject = 'Here is the subject';
- $mail->Body = 'This is the HTML message body <b>in bold!</b>';
- $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
- $mail->send();
- echo 'Message has been sent';
- } catch (Exception $e) {
- echo 'Message could not be sent.';
- echo 'Mailer Error: ' . $mail->ErrorInfo;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement