Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.32 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 = "phpemailresponser@gmail.com";
  7. const SMTP_PASSWORD = "mQWewpe8";
  8. const SMTP_FROM_EMAIL = "phpemailresponser@gmail.com";
  9. const SMTP_FROM_NAME = "hanacakes.sk";
  10. const SMTP_TARGET_EMAIL = "info@hanacakes.sk";
  11. const SMTP_CC_EMAIL = "infohanacakes@gmail.com";
  12. const SMTP_HOST = "smtp.gmail.com";
  13. const SMTP_SUBJECT = "Objednávka [Torta] - Hana Cakes";
  14. //enable preflight request
  15. if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  16. http_response_code(200);
  17. exit();
  18. }
  19. require './phpmailer/PHPMailerAutoload.php';
  20. //test request is valid, if not return 400 - bad request
  21. if (!isRequestValid()) {
  22.     http_response_code(400);
  23.     echo "Invalid request";
  24.     exit();
  25. }
  26. $name = $_POST['name'];
  27. $email = $_POST['email'];
  28. $phone = $_POST['phone'];
  29. $date = $_POST['date'];
  30. $floors = $_POST['floors'];
  31. $average = $_POST['average'];
  32. $taste = $_POST['taste'];
  33. $korpus = $_POST['korpus'];
  34. $cream = $_POST['cream'];
  35. $gel = $_POST['gel'];
  36. $message = $_POST['message'];
  37. if (!isDataValid($name, $email, $phone, $date, $floors, $average, $taste, $korpus, $cream, $gel, $message)) {
  38.     http_response_code(400);
  39.     echo "Invalid data";
  40.     exit();
  41. }
  42. sendMail($name, $email, $phone, date('d.m.Y', strtotime($date)), $floors, $average, $taste, $korpus, $cream, $gel, $message);
  43. /*************************************************************************/
  44. /**
  45.  * Test request method is POST and content type is application/json
  46.  */
  47. function isRequestValid() {
  48.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  49.         return false;
  50.     }
  51.     return true;
  52. }
  53. function isDataValid($name, $email, $phone, $date, $floors, $average, $taste, $korpus, $cream, $gel, $message) {
  54.     if (empty($name)) {
  55.         return false;
  56.     }
  57.     if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  58.             return false;
  59.         }
  60.     if (empty($phone)) {
  61.         return false;
  62.     }
  63.     if (empty($date)) {
  64.         return false;
  65.     }
  66.     if (empty($floors)) {
  67.         return false;
  68.     }
  69.     if (empty($average)) {
  70.         return false;
  71.     }
  72.     if (empty($taste)) {
  73.         return false;
  74.     }
  75.     if (empty($korpus)) {
  76.         return false;
  77.     }
  78.     if (empty($cream)) {
  79.         return false;
  80.     }
  81.     if (empty($gel)) {
  82.         return false;
  83.     }
  84.     if (empty($message)) {
  85.         return false;
  86.     }
  87.     return true;
  88. }
  89. function sendMail($name, $email, $phone, $date, $floors, $average, $taste, $korpus, $cream, $gel, $message) {
  90.     $mail = new PHPMailer;
  91.     $mail->SMTPDebug = 0;                               // Enable verbose debug output
  92.     $mail->isSMTP();                                      // Set mailer to use SMTP
  93.     $mail->CharSet="UTF-8";
  94.     $mail->Host = SMTP_HOST;  // Specify main and backup SMTP servers
  95.     $mail->SMTPAuth = true;                               // Enable SMTP authentication
  96.     $mail->Username = SMTP_USERNAME;                      // SMTP username
  97.     $mail->Password = SMTP_PASSWORD;                      // SMTP password
  98.     $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
  99.     $mail->Port = 587;                                    // TCP port to connect to
  100.     $mail->setFrom(SMTP_FROM_EMAIL, SMTP_FROM_NAME);
  101.   $mail->addAddress(SMTP_TARGET_EMAIL);                   // Add a recipient
  102.   $mail->addCC(SMTP_CC_EMAIL);                      // Add a copy recipient
  103.   $mail->addBcc(SMTP_BCC_EMAIL);                    // Add secret recipient
  104.     $mail->addReplyTo($email, $name);
  105.     $mail->isHTML(true);                                  // Set email format to HTML
  106.     $mail->Subject = SMTP_SUBJECT;
  107.     $mail->Body = "<b>Správa od:</b><br>$name<br>$phone<br>$email<br><b>Dátum vybavenie objednávky:</b><br>$date<br><br><b>Počet poschodí torty:</b><br>$floors<br><b>Priemer torty:</b><br>$average<br><b>Torta:</b><br>$taste<br><b>Korpus:</b><br>$korpus<br><b>Krém:</b><br>$cream<br><b>Gél:</b><br>$gel<br><br><b>Obsah správy:</b><br>$message";
  108.     if (isset($_FILES['file'])) {
  109.         $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
  110.     }
  111.     if(!$mail->send()) {
  112.         http_response_code(500);
  113.         echo "Encountered error while sending email.";
  114.         exit();
  115.     } else {
  116.         http_response_code(200);
  117.     }
  118. }
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement