Advertisement
Guest User

Untitled

a guest
May 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.72 KB | None | 0 0
  1. <?php
  2.     // Only process POST reqeusts.
  3.     if ($_SERVER["REQUEST_METHOD"] == "POST") {
  4.         // Get the form fields and remove whitespace.
  5.         $name = trim($_POST["name"]);
  6.         $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
  7.         $subject = trim($_POST["subject"]);
  8.         $message = trim($_POST["message"]);
  9.        
  10.         // If empty or bad e-mail
  11.         if (empty($name) OR empty($subject) OR empty($message)
  12.                 OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  13.             // Set a 400 (bad request) response code and exit.
  14.             http_response_code(400);
  15.             echo "Oops! There was a problem with your submission. Please complete the form and try again.";
  16.             exit;
  17.         }
  18.  
  19.        
  20.         $recipient = "solarenergynet@gmail.com";
  21.         $subject = "[CONTACT] $subject";
  22.  
  23.         // Build the email content.
  24.         $email_content = "Name: $name\n";
  25.         $email_content .= "E-mail: $email\n";
  26.         $email_content .= "Message:\n$message\n";
  27.         // Build the email headers.
  28.         $email_headers = "From: $name <$email>";
  29.  
  30.         // Send the email.
  31.         if (mail($recipient, $subject, $email_content, $email_headers)) {
  32.             // Set a 200 (okay) response code.
  33.             http_response_code(200);
  34.             echo "Thank You! Your message has been sent.";
  35.         } else {
  36.             // Set a 500 (internal server error) response code.
  37.             http_response_code(500);
  38.             echo "Oops! Something went wrong and we couldn't send your message.";
  39.         }
  40.  
  41.     } else {
  42.         // Not a POST request, set a 403 (forbidden) response code.
  43.         http_response_code(403);
  44.         echo "There was a problem with your submission, please try again.";
  45.     }
  46.  
  47. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement