Advertisement
Guest User

Untitled

a guest
Dec 18th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. <?php
  2. //require 'PHPMailer-master/PHPMailerAutoload.php';
  3. header('Content-Type: application/json');
  4.  
  5. $errors = array();
  6.  
  7. $title = (isset($_POST['title']) && ($_POST['title'] != '0')) ? $_POST['title'] : null;
  8. $firstName = (isset($_POST['firstName']) && !(empty($_POST["firstName"]))) ? $_POST['firstName'] : null;
  9. $lastName = (isset($_POST['lastName']) && !(empty($_POST["lastName"]))) ? $_POST['lastName'] : null;
  10. $emailAddress = (isset($_POST['emailAddress']) && !(empty($_POST["emailAddress"]))) ? $_POST['emailAddress'] : null;
  11. $website = isset($_POST['website']) ? $_POST['website'] : '';
  12. $message = (isset($_POST['message']) && !(empty($_POST["message"]))) ? $_POST['message'] : null;
  13.  
  14. if ($title === null) {
  15. $errors[] = 'You must select a title';
  16. }
  17.  
  18. if ($firstName === null) {
  19. $errors[] = 'You must enter a first name';
  20. }
  21.  
  22. if ($lastName === null) {
  23. $errors[] = 'You must enter a last name';
  24. }
  25.  
  26. if ($emailAddress === null || filter_var($emailAddress, FILTER_VALIDATE_EMAIL) === false) {
  27. $errors[] = 'You must enter a valid email address';
  28. }
  29.  
  30. if ($website !== '') {
  31. if(strpos($website, '://') === false) {
  32. $website = 'http://' . $website;
  33. }
  34.  
  35. if (filter_var($website, FILTER_VALIDATE_URL, array('flags' => null)) === false) {
  36. $errors[] = 'You must enter a valid website address';
  37. }
  38. }
  39.  
  40. if ($message === null) {
  41. $errors[] = 'You must enter a message';
  42. }
  43.  
  44. if (empty($errors)) {
  45. require __DIR__ . '/PHPMailer-master/PHPMailerAutoload.php';
  46.  
  47. //SMTP needs accurate times, and the PHP time zone MUST be set
  48. //This should be done in your php.ini, but this is how to do it if you don't have access to that
  49. date_default_timezone_set('Etc/UTC');
  50.  
  51. $mail = new PHPMailer;
  52. $mail->isSMTP();
  53. $mail->Host = 'smtp.gmail.com';
  54. $mail->Port = 587;
  55. $mail->SMTPSecure = 'tls';
  56. $mail->SMTPAuth = true;
  57. $mail->Username = "[email protected]";
  58. $mail->Password = "YOUR APP SPESIFIC PASSWORD";
  59.  
  60. $mail->setFrom($emailAddress, $firstName . ' ' . $lastName);
  61. $mail->addAddress('[email protected]', 'Bassa Basso');
  62. $mail->Subject = 'New request';
  63. $mail->Body = $message . "\r\n\r\nWebsite: " . $website;
  64.  
  65. if ($mail->send()) {
  66. echo json_encode('Form submitted successfully!');
  67. exit();
  68. }
  69.  
  70. // You may want to log this somewhere
  71. // $mail->ErrorInfo;
  72.  
  73. $errors[] = 'Could not send email, please try again later';
  74. }
  75.  
  76. http_response_code(400);
  77. echo json_encode($errors);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement