Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. <?php
  2. require 'config.php';
  3. require 'Slim/Slim.php';
  4.  
  5.  
  6. use PHPMailerPHPMailerPHPMailer;
  7. use PHPMailerPHPMailerException;
  8.  
  9. require 'vendor/autoload.php';
  10.  
  11.  
  12. SlimSlim::registerAutoloader();
  13. $app = new SlimSlim();
  14.  
  15. $app->post('/login','login'); /* User login */
  16. $app->get('sendMail','sendMail'); /* send Email */
  17.  
  18.  
  19. $app->run();
  20.  
  21. /************************* USER LOGIN *************************************/
  22. /* ### User login ### */
  23. function login() {
  24.  
  25. $request = SlimSlim::getInstance()->request();
  26. $data = json_decode($request->getBody());
  27.  
  28. try {
  29.  
  30. $db = getDB();
  31. $userData ='';
  32. $sql = "SELECT user_id, name, email, username FROM users WHERE (username=:username or email=:username) and password=:password ";
  33. $stmt = $db->prepare($sql);
  34. $stmt->bindParam("username", $data->username, PDO::PARAM_STR);
  35. $password=hash('sha256',$data->password);
  36. $stmt->bindParam("password", $password, PDO::PARAM_STR);
  37. $stmt->execute();
  38. $mainCount=$stmt->rowCount();
  39. $userData = $stmt->fetch(PDO::FETCH_OBJ);
  40.  
  41. if(!empty($userData))
  42. {
  43. $user_id=$userData->user_id;
  44. $userData->token = apiToken($user_id);
  45. }
  46.  
  47. $db = null;
  48. if($userData){
  49. $userData = json_encode($userData);
  50. echo '{"userData": ' .$userData . '}';
  51. } else {
  52. echo '{"error":{"text":"Bad request wrong username and password"}}';
  53. }
  54.  
  55.  
  56. }
  57. catch(PDOException $e) {
  58. echo '{"error":{"text":'. $e->getMessage() .'}}';
  59. }
  60. }
  61.  
  62.  
  63. /* ### send email test ### */
  64.  
  65. function sendMail()
  66. {
  67.  
  68. // below credential is my actual mailtrap credential
  69.  
  70.  
  71.  
  72. $mail = new PHPMailer(true);
  73. try {
  74. //Server settings
  75. $mail->SMTPDebug = 2; // Enable verbose debug output
  76. $mail->isSMTP(); // Set mailer to use SMTP
  77. $mail->Host = 'smtp.mailtrap.io'; // Specify main and backup SMTP servers
  78. $mail->SMTPAuth = true; // Enable SMTP authentication
  79. $mail->Username = '1fd562fc56d940'; // SMTP username
  80. $mail->Password = 'af5ec48a984df1'; // SMTP password
  81. $mail->SMTPSecure = 'PLAIN'; // Enable TLS encryption, `ssl` also accepted
  82. $mail->Port = 2525; // TCP port to connect to
  83.  
  84. //Recipients
  85. $mail->setFrom('from@example.com', 'Mailer');
  86. $mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
  87. // $mail->addAddress('ellen@example.com'); // Name is optional
  88. // $mail->addReplyTo('info@example.com', 'Information');
  89. // $mail->addCC('cc@example.com');
  90. // $mail->addBCC('bcc@example.com');
  91.  
  92. //Attachments
  93. // $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
  94. // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
  95.  
  96. //Content
  97. $mail->isHTML(true); // Set email format to HTML
  98. $mail->Subject = 'Here is the subject';
  99. $mail->Body = 'This is the HTML message body <b>in bold!</b>';
  100. $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  101.  
  102. $mail->send();
  103. echo 'Message has been sent';
  104. } catch (Exception $e) {
  105. echo 'Message could not be sent.';
  106. echo 'Mailer Error: ' . $mail->ErrorInfo;
  107. }
  108.  
  109. }
  110.  
  111.  
  112. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement