Posted By

dubogii on 05/17/11


Tagged

email send


Versions (?)

Send Email


 / Published in: PHP
 

URL: http://css-tricks.com/snippets/php/send-email/

  1. 1) HTML Form with Inputs
  2.  
  3. <form action="" method="post">
  4. <label for="name">Name:</label>
  5. <input type="text" name="name" id="name" />
  6.  
  7. <label for="Email">Email:</label>
  8. <input type="text" name="email" id="email" />
  9.  
  10. <label for="Message">Message:</label><br />
  11. <textarea name="message" rows="20" cols="20" id="message"></textarea>
  12.  
  13. <input type="submit" name="submit" value="Submit" />
  14. </form>
  15.  
  16.  
  17.  
  18. 2) Process with PHP
  19.  
  20. This could be in a seperate file (e.g. sendemail.php) in which you'd set the action URL of the form to go there. Or, have the form submit to itself (leave action URL blank) and test for one of the values of the form being POSTed and process there.
  21.  
  22. <?php
  23. // from the form
  24. $name = trim(strip_tags($_POST['name']));
  25. $email = trim(strip_tags($_POST['email']));
  26. $message = htmlentities($_POST['message']);
  27.  
  28. // set here
  29. $subject = "Contact form submitted!";
  30. $to = '[email protected]';
  31.  
  32. $body = <<<HTML
  33. $message
  34. HTML;
  35.  
  36. $headers = "From: $email
  37. ";
  38. $headers .= "Content-type: text/html
  39. ";
  40.  
  41. // send the email
  42. mail($to, $subject, $body, $headers);
  43.  
  44. // redirect afterwords, if needed
  45. header('Location: thanks.html');
  46. ?>
  47.  
  48.  
  49.  
  50. 3) Test it
  51.  
  52. And make sure to keep up with security news around the web.

Report this snippet  

You need to login to post a comment.