Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3.  
  4.     // Only process POST requests.
  5.     if ($_SERVER["REQUEST_METHOD"] == "POST") {
  6.         // Get the form fields and remove white space.
  7.         $first_name = strip_tags(trim($_POST["first_name"]));
  8.         $first_name = str_replace(array("\r","\n"),array(" "," "),$first_name);
  9.         $last_name = trim($_POST["last_name"]);
  10.         $email = filter_var(trim($_POST["email_address"]), FILTER_SANITIZE_EMAIL);
  11.         $phone = trim($_POST["phone_no"]);
  12.         $message = trim($_POST["con_message"]);
  13.  
  14.         // Check that data was sent to the mailer.
  15.         if ( empty($first_name) OR ( empty($last_name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  16.             // Set a 400 (bad request) response code and exit.
  17.             http_response_code(400);
  18.             echo "Please complete the form and try again.";
  19.             exit;
  20.         }
  21.  
  22.         // Set the recipient email address.
  23.         $recipient = "rajuofficialemail@gmail.com";
  24.  
  25.         // Set the email subject.
  26.         $subject = "Businex - Mail From $first_name";
  27.  
  28.         // Build the email content.
  29.         $email_content = "Name: $first_name + $last_name\n";
  30.         $email_content .= "Phone: $phone\n\n";
  31.         $email_content .= "Email: $email\n\n";
  32.         $email_content .= "Message:\n$message\n";
  33.  
  34.         // Build the email headers.
  35.         $email_headers = "From: $first_name <$email>";
  36.  
  37.         // Send the email.
  38.         if (mail($recipient, $subject, $email_content, $email_headers)) {
  39.             // Set a 200 (okay) response code.
  40.             http_response_code(200);
  41.             echo "Thank You! Your message has been sent.";
  42.         } else {
  43.             // Set a 500 (internal server error) response code.
  44.             http_response_code(500);
  45.             echo "Oops! Something went wrong and we couldn't send your message.";
  46.         }
  47.  
  48.     } else {
  49.         // Not a POST request, set a 403 (forbidden) response code.
  50.         http_response_code(403);
  51.         echo "There was a problem with your submission, please try again.";
  52.     }
  53.  
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement