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.  
  31. $body = <<<HTML
  32. $message
  33. HTML;
  34.  
  35. $headers = "From: $email
  36. ";
  37. $headers .= "Content-type: text/html
  38. ";
  39.  
  40. // send the email
  41. mail($to, $subject, $body, $headers);
  42.  
  43. // redirect afterwords, if needed
  44. header('Location: thanks.html');
  45. ?>
  46.  
  47.  
  48.  
  49. 3) Test it
  50.  
  51. And make sure to keep up with security news around the web.

Report this snippet  

You need to login to post a comment.