PHP e-mail Validator and sender


/ Published in: PHP
Save to your folder(s)

Form needs to call the page like this:


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // CONFIGURATION --------------------------------------------------------------
  4.  
  5. // This is the email where the contact mails will be sent to.
  6. $config['recipient'] = '[email protected]';
  7.  
  8. // This is the subject line for contact emails.
  9. // The variable %name% will be replaced with the name of the sender.
  10. $config['subject'] = 'Contact message from %name%';
  11.  
  12. // These are the messages displayed in case of form errors.
  13. $config['errors'] = array
  14. (
  15. 'no_name' => 'Please enter your name',
  16. 'no_email' => 'Your Email adresse is required.',
  17. 'invalid_email' => 'You entered an invalid email address.',
  18. 'no_message' => 'Please, include your message.',
  19. );
  20.  
  21. // END OF CONFIGURATION -------------------------------------------------------
  22.  
  23.  
  24. // Ignore non-POST requests
  25. if ( ! $_POST)
  26. exit('Nothing to see here. Please go back to the site.');
  27.  
  28. // Was this an AJAX request or not?
  29. $ajax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
  30.  
  31. // Set the correct HTTP headers
  32. header('Content-Type: text/'.($ajax ? 'plain' : 'html').'; charset=utf-8');
  33.  
  34. // Extract and trim contactform values
  35. $name = isset($_POST['name']) ? trim($_POST['name']) : '';
  36. $email = isset($_POST['email']) ? trim($_POST['email']) : '';
  37. $message = isset($_POST['message']) ? trim($_POST['message']) : '';
  38.  
  39. // Take care of magic quotes if needed (you really should have them disabled)
  40. {
  41. $name = stripslashes($name);
  42. $email = stripslashes($email);
  43. $message = stripslashes($message);
  44. }
  45.  
  46. // Initialize the errors array which will also be sent back as a JSON object
  47. $errors = NULL;
  48.  
  49. // Validate name
  50. if ($name == '' || strpos($name, "\r") || strpos($name, "\n"))
  51. {
  52. $errors['name'] = $config['errors']['no_name'];
  53. }
  54.  
  55. // Validate email
  56. if ($email == '')
  57. {
  58. $errors['email'] = $config['errors']['no_email'];
  59. }
  60. elseif ( ! preg_match('/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD', $email))
  61. {
  62. $errors['email'] = $config['errors']['invalid_email'];
  63. }
  64.  
  65. // Validate message
  66. if ($message == '')
  67. {
  68. $errors['message'] = $config['errors']['no_message'];
  69. }
  70.  
  71. // Validation succeeded
  72. if (empty($errors))
  73. {
  74. // Prepare subject line
  75. $subject = str_replace('%name%', $name, $config['subject']);
  76.  
  77. // Additional mail headers
  78. $headers = 'Content-Type: text/plain; charset=utf-8'."
  79. ";
  80. $headers .= 'From: '.$email;
  81.  
  82. // Send the mail
  83. if ( ! mail($config['recipient'], $subject, $message, $headers))
  84. {
  85. $errors['server'] = 'There seems to be a technical problem with our server. We are sorry. '.
  86. 'Could you mail your message directly at '.$config['recipient'].'? Thank you.';
  87. }
  88. }
  89.  
  90. if ($ajax)
  91. {
  92. // Output the possible errors as a JSON object
  93. echo json_encode($errors);
  94. }
  95. else
  96. {
  97. // Show a simple HTML feedback message in case of non-javascript support
  98. if (empty($errors))
  99. {
  100. echo '<h1>Thank you</h1>';
  101. echo '<p>Your message has been sent.</p>';
  102. }
  103. else
  104. {
  105. echo '<h1>Oops!</h1>';
  106. echo '<p>Please go back and fix the following errors:</p>';
  107. echo '<ul><li>';
  108. echo implode('</li><li>', $errors);
  109. echo '</li></ul>';
  110. }
  111. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.