Advertisement
JonneOpettaja

Untitled

Nov 4th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. // Define some constants
  4. define( "RECIPIENT_NAME", "John Smith" );
  5. define( "RECIPIENT_EMAIL", "john@example.com" );
  6. define( "EMAIL_SUBJECT", "Visitor Message" );
  7.  
  8. // Read the form values
  9. $success = false;
  10. $senderName = isset( $_POST['senderName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderName'] ) : "";
  11. $senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";
  12. $message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
  13.  
  14. // If all values exist, send the email
  15. if ( $senderName && $senderEmail && $message ) {
  16.   $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  17.   $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  18.   $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
  19. }
  20.  
  21. // Return an appropriate response to the browser
  22. if ( isset($_GET["ajax"]) ) {
  23.   echo $success ? "success" : "error";
  24. } else {
  25. ?>
  26. <html>
  27.   <head>
  28.     <title>Thanks!</title>
  29.   </head>
  30.   <body>
  31.   <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>
  32.   <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
  33.   <p>Click your browser's Back button to return to the page.</p>
  34.   </body>
  35. </html>
  36. <?php
  37. }
  38. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement