A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Home » Code Snippets » PHP » Send Email Submit one!

Send Email

1) HTML Form with Inputs

<form action="" method="post">
  <label for="name">Name:</label>
  <input type="text" name="name" id="name" />

  <label for="Email">Email:</label>
  <input type="text" name="email" id="email" />

  <label for="Message">Message:</label><br />
  <textarea name="message" rows="20" cols="20" id="message"></textarea>

  <input type="submit" name="submit" value="Submit" />
</form>

2) Process with PHP

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.

<?php
       // from the form
       $name = trim(strip_tags($_POST['name']));
       $email = trim(strip_tags($_POST['email']));
       $message = htmlentities($_POST['message']);

       // set here
       $subject = "Contact form submitted!";
       $to = 'your@email.com';

       $body = <<<HTML
$message
HTML;

       $headers = "From: $email\r\n";
       $headers .= "Content-type: text/html\r\n";

       // send the email
       mail($to, $subject, $body, $headers);

       // redirect afterwords, if needed
       header('Location: thanks.html');
?>

3) Test it

And make sure to keep up with security news around the web.

Subscribe to The Thread

  1. thanks this was real helpful !
    as I’m quite new to PHP.

  2. Andy Jones says:

    Hiya – great stuff as always – but for some reason, I can’t get this to work! It redirects to me thanks page, but the email doesn’t come through. Is there something special that has to be configured in php? I’m running php4 (but I don’t really get too involved in php stuff!!)

    Lovin your work on twitter by the way!

  3. imeera says:

    great! – but for some reason, It redirects to me thanks page, but the email comes blank no data in there. any help would be appreciated. I am getting in to php.

  4. Leon says:

    Thanks Chris, it is really helpfull

  5. Blair says:

    still can’t get this to work. using wordpress.

  6. Blair says:

    I get this error: Warning: Cannot modify header information – headers already sent by (output started at /home2/yourmedp/public_html/wp-content/themes/obvious/inc/contact-form.php:3) in /home2/yourmedp/public_html/wp-content/themes/obvious/inc/contact-form.php on line 24

  7. varun says:

    Can U Tell Me Solution For Bulk Mail ??

  8. Danny says:

    Replace the following in the php bit:

    $body = <<<HTML
    $message
    HTML;

    with:

    $body = $message;

    And it should work.

  9. Liam says:

    Hi, thanks for this code, i was pulling my hair out trying to get my form to work! i did have some problems though, you need to change the code as mentioned above^^^^^ and also I was not receiving the persons name in the email so i substituted “Contact form submitted!” for “$name” and now their name comes through in the header :-)

    Thanks again, Liam

  10. It work for after some tweak:

    put the input form and textarea names and ids lowercase.

    But i can not use it like this.

  11. Stephen says:

    I’m not sure how to link the PHP with the HTML. Could someone email me?

  12. Ayo says:

    Just getting warmed up on the development level of web education.. need this like last year.
    Very useful, thank you.

  13. Ayo says:

    I got issues testing from a local server. How can I make it work please?.

  14. Ayo says:

    I understand I could download a mail server. Any ideas?

  15. It only works if you write exactly the same in the variable name ($Name) as the in the input tag in your html document. I made it work by the copy-paste method so I was sure that the variables had the same name..

    In the code example the name is written with capitalize and in the php code the variable name is written with lowercase, and that courses the problem.

    Hope it helped..

  16. Arthur says:

    Hey Chris,

    Thanks for this form. Though for some reason when I put in an email address the form doesn’t get submitted properly. But, if I leave the email blank or leave out the @ it works fine. Do you have any suggestions?

    Cheers!

  17. dave says:

    In the php code, is the period after the second “$headers” a mistake? or does it need to be there?

  18. andyzzy says:

    hi
    sorry, im italian and what mean <<<HTML HTML; ?

    i have seen this in other places.

  19. daniar says:

    why when I run this source

    results are always :
    Warning: mail() [function.mail]: Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set().

  20. you are the only one who put this task into terms that actually worked for me…very easy to understand. Thanks!

  21. Jim says:
    <?php
           // from the form
           $name = htmlentities($_POST['name']);
    	   $title = htmlentities($_POST['title']);
    	   $company = htmlentities($_POST['company']);
           $email = htmlentities($_POST['email']);
    	   $phone = htmlentities($_POST['phone']);
    	   $fax = htmlentities($_POST['fax']);
           $address1 = htmlentities($_POST['address1']);
    	   $address2 = htmlentities($_POST['address2']);
    	   $city = htmlentities($_POST['city']);
    	   $zip = htmlentities($_POST['zip']);
    	   $country = htmlentities($_POST['country']);
    	   $needs = htmlentities($_POST['needs']);   
    
           // set here
           $subject = "Incoming Information Request";
           $to = 'info@gauging.com';
    
           $message="Incoming Information Request\n\n";
    		$message .="From: $name\n";
    		$message .="Title: $title\n";
    		$message .="Company: $company\n";
    		$message .="Email: $email\n";
    		$message .="Phone: $phone\n";
    		$message .="Fax: $fax\n";
    		$message .="Address: $address1\n";
    		$message .="Address: $address2\n";
    		$message .="City: $city\n";
    		$message .="Zip: $zip\n";
    		$message .="Country: $country\n";
    		$message .="Message: $needs\n";
    
           $headers = "From: $email\r\n";
           $headers .= "Content-Type: text/plain;\n";
    
           // send the email
           mail($to, $subject, $message, $headers);
    
           // redirect afterwords, if needed
           header('Location: thanks.html');
    ?>

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---