Advertisement
Guest User

php

a guest
Mar 4th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. <?php
  2.  
  3. //Retrieve form data.
  4. //GET - user submitted data using AJAX
  5. //POST - in case user does not support javascript, we'll use POST instead
  6. $name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
  7. $email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
  8. $comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
  9.  
  10. //flag to indicate which method it uses. If POST set it to 1
  11.  
  12. if ($_POST) $post=1;
  13.  
  14. //Simple server side validation for POST data, of course, you should validate the email
  15. if (!$name) $errors[count($errors)] = 'Please enter your name.';
  16. if (!$email) $errors[count($errors)] = 'Please enter your email.';
  17. if (!$comment) $errors[count($errors)] = 'Please enter your comment.';
  18.  
  19. //if the errors array is empty, send the mail
  20. if (!$errors) {
  21.  
  22. //recipient - replace your email here
  23. $to = 'redacted';
  24. //sender - from the form
  25. $from = $name . ' <' . $email . '>';
  26.  
  27. //subject and the html message
  28. $subject = 'Message from ' . $name;
  29. $message = 'Name: ' . $name . '<br/><br/>
  30. Email: ' . $email . '<br/><br/>
  31. Message: ' . nl2br($comment) . '<br/>';
  32.  
  33. //send the mail
  34. $result = sendmail($to, $subject, $message, $from);
  35.  
  36. //if POST was used, display the message straight away
  37. if ($_POST) {
  38. if ($result) echo 'Thank you! We have received your message.';
  39. else echo 'Sorry, unexpected error. Please try again later';
  40.  
  41. //else if GET was used, return the boolean value so that
  42. //ajax script can react accordingly
  43. //1 means success, 0 means failed
  44. } else {
  45. echo $result;
  46. }
  47.  
  48. //if the errors array has values
  49. } else {
  50. //display the errors message
  51. for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
  52. echo '<a href="index.html">Back</a>';
  53. exit;
  54. }
  55.  
  56.  
  57. //Simple mail function with HTML header
  58. function sendmail($to, $subject, $message, $from) {
  59. $headers = "MIME-Version: 1.0" . "\r\n";
  60. $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  61. $headers .= 'From: ' . $from . "\r\n";
  62.  
  63. $result = mail($to,$subject,$message,$headers);
  64.  
  65. if ($result) return 1;
  66. else return 0;
  67. }
  68.  
  69. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement