Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. <?php
  2. header('Content-Type: multipart/form-data');
  3. header('Access-Control-Allow-Origin: *');
  4. header('Access-Control-Allow-Methods: DELETE, HEAD, GET, OPTIONS, POST, PUT');
  5. header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
  6. const SMTP_USERNAME = "";
  7. const SMTP_PASSWORD = "";
  8. const SMTP_FROM_EMAIL = "";
  9. const SMTP_FROM_NAME = "";
  10. const SMTP_TARGET_EMAIL = "";
  11. const SMTP_CC_EMAIL = "";
  12. const SMTP_HOST = "";
  13. const SMTP_SUBJECT = "";
  14. // enable preflight request
  15. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  16.     http_response_code(200);
  17.     exit();
  18. }
  19. // require PHPMailer
  20. require './phpmailer/PHPMailerAutoload.php';
  21. //test request is valid, if not return 400 - bad request
  22. if (!isRequestValid()) {
  23.     http_response_code(400);
  24.     echo "Invalid request";
  25.     exit();
  26. }
  27. // collect POST data from request
  28. $name = $_POST['name'];
  29. $email = $_POST['email'];
  30. $phone = $_POST['phone'];
  31. $message = $_POST['message'];
  32. if (!isDataValid($name, $email, $phone, $message)) {
  33.     http_response_code(400);
  34.     echo "Invalid data";
  35.     exit();
  36. }
  37. // Send email function call
  38. sendMail($name, $email, $phone, $message);
  39. // Check if request method is POST
  40. function isRequestValid() {
  41.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  42.         return false;
  43.     }
  44.     return true;
  45. }
  46. function isDataValid($name, $email, $phone, $message) {
  47.     if (empty($name)) {
  48.         return false;
  49.     }
  50.     if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  51.         return false;
  52.     }
  53.     if (empty($phone)) {
  54.         return false;
  55.     }
  56.     if (empty($message)) {
  57.         return false;
  58.     }
  59.     return true;
  60. }
  61. function sendMail($name, $email, $phone, $message) {
  62.     $mail = new PHPMailer;
  63.     $mail->SMTPDebug = 0;
  64.     $mail->isSMTP();
  65.     $mail->CharSet="UTF-8";
  66.     $mail->Host = SMTP_HOST;
  67.     $mail->SMTPAuth = true;
  68.     $mail->Username = SMTP_USERNAME;
  69.     $mail->Password = SMTP_PASSWORD;
  70.     // TLS || SSL
  71.     $mail->SMTPSecure = 'tls';
  72.     // TCP PORT
  73.     $mail->Port = 587;
  74.     $mail->setFrom(SMTP_FROM_EMAIL, SMTP_FROM_NAME);
  75.     $mail->addAddress(SMTP_TARGET_EMAIL);
  76.     $mail->addCC(SMTP_CC_EMAIL);
  77.     $mail->addBcc(SMTP_BCC_EMAIL);
  78.     $mail->addReplyTo($email, $name);
  79.     // Set email body content to HTML so you can embed HTML tags like <h1></h1>, <br>, etc.
  80.     $mail->isHTML(true);
  81.     $mail->Subject = SMTP_SUBJECT;
  82.     $mail->Body = "MENO: $name, EMAIL: $email, PHONE: $phone, MESSAGE: $message";
  83.     if(!$mail->send()) {
  84.         http_response_code(500);
  85.         echo "Encountered error while sending email.";
  86.         exit();
  87.     } else {
  88.         http_response_code(200);
  89.     }
  90. }
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement