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" />

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.

       // 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 = '';

       $body = <<<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.


  1. User Avatar
    Rob Salaman
    Permalink to comment#

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

  2. User Avatar
    Andy Jones
    Permalink to comment#

    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. User Avatar
    Permalink to comment#

    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.

    • User Avatar
      Alexandru Nastase
      Permalink to comment#

      I’m getting the same problem it’s like is not reading the inputs

    • User Avatar
      Alexandru Nastase
      Permalink to comment#

      got the problem solved the form method must be POST (i’m dumber in my way :)) )
      Thanks Chris again and again and yeah AGAIN THANKS !!

  4. User Avatar
    Permalink to comment#

    Thanks Chris, it is really helpfull

  5. User Avatar
    Permalink to comment#

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

  6. User Avatar
    Permalink to comment#

    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. User Avatar
    Permalink to comment#

    Can U Tell Me Solution For Bulk Mail ??

    • User Avatar

      You can use foreach or while function to send an email to each user.
      foreach ($mails as $mail=>$email) {
      mail($email, $subject, $body, $headers);

  8. User Avatar
    Permalink to comment#

    Replace the following in the php bit:

    $body = <<<HTML


    $body = $message;

    And it should work.

  9. User Avatar
    Permalink to comment#

    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. User Avatar
    José Monteiro
    Permalink to comment#

    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. User Avatar
    Permalink to comment#

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

  12. User Avatar
    Permalink to comment#

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

  13. User Avatar
    Permalink to comment#

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

  14. User Avatar
    Permalink to comment#

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

  15. User Avatar
    Sebastian Dreijer
    Permalink to comment#

    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..

    • User Avatar
      Chris Coyier
      Permalink to comment#

      Ah yes! That’s definitely a problem. This is now fixed in the above code.

  16. User Avatar
    Permalink to comment#

    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?


  17. User Avatar
    Permalink to comment#

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

    • User Avatar
      Chris Coyier
      Permalink to comment#

      That means: “append this to the existing string”

  18. User Avatar
    Permalink to comment#

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

    i have seen this in other places.

  19. User Avatar

    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. User Avatar
    Chris Clowers

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

  21. User Avatar
           // 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 = '';
           $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');
  22. User Avatar
    Permalink to comment#

    hey guyz, this isnt working for me :( , did everything and i also replaced the code as Danny said above … but im not getting any email =/ … any guesses why??

  23. User Avatar
    Permalink to comment#

    hey the stuff runs well but their is no mail posted to my gmail. what is the problem

  24. User Avatar
    Permalink to comment#

    This code would work, but is extremely unsecure, and people are able to hack the email system. I would recommend looking at the following tutorial, although it should not be followed exactly as w3schools is kind of unreliable

    Anyway, this code is nice for a base!

  25. User Avatar
    Permalink to comment#

    Is there a way to make an input field in this form not print if it’s an empty field?

  26. User Avatar
    Permalink to comment#

    Hey guys,

    I, like someone previously mentioned, am testing this on a local server setup to send it to my personal gmail, but Im not getting anything to show up. I’ve read on a couple other posts here that sometimes it can take a while because of that – – – – > or am I waiting needlessly because its actually not working.

    Any feedback would be awesome =)

    // G //

  27. User Avatar
    Permalink to comment#

    What is the change in above code… there is no email submission ….i try to send perfect mail copy to me and user…. and what about config setting ?

  28. User Avatar
    Permalink to comment#

    I’m a novice to php. I get emails comeing through fine but the I get a webpage error ‘The webpage cannot be found’ once the submit has been executed. Can anyone help?

  29. User Avatar
    Jon Kempf
    Permalink to comment#

    Since I am not a skillful operator in the world of code, I cannot get this form to work for me. Right now, it sends the form to my email, but there is no message in the body of the email. I replaced the

    $body = $message;

    but still no luck.
    I also have the form method set to “post” and the action is pointing to the php file. Anyone wanna try and take a stab at this, or was it answered above and I am just not getting it?

  30. User Avatar
    Permalink to comment#

    I’m just at the beginning of php so i don’t get it all,

    But i’m getting a error on the page , pls help

    This is the error :

    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() in D:\wamp\www\contact.php on line 19

  31. User Avatar

    I am new to PHP, I do not understand where i will start. Is there any whow can help me?

    Thank you!

  32. User Avatar
    Permalink to comment#

    Hasem it is very easy, first create a php file, for example, index.php and a process file with name form_process.php. Keep these files inside one folder.

    Now copy the above HTML code and paste it in index.php and and between the action attribute add form_process.php.

    In the form_process.php file paste the above PHP code, that’s it. If you are testing the code on local server, then use “Test Mail Server For localhost” for email testing.


  33. User Avatar
    Hamza Khan
    Permalink to comment#

    @Chris Coyier
    thanks for this post.
    Useful and simple kinda snippet.
    Earned 10$ from this snippet post.

  34. User Avatar
    Marcelo Silva
    Permalink to comment#

    You sent the email without to change the SMTP PORT to 597. By default it’s 25 ?

  35. User Avatar
    aryo tunjung kusumo
    Permalink to comment#

    how to send email if the recipient is more than 1000?

  36. User Avatar
    Permalink to comment#

    Tried it out on my server however I get this error:

    Parse error: syntax error, unexpected $end in /home/content/41/8892341/html/toastdispatch/Apps/EmailPHPTest/emailr.php on line 25

    Any help would be nice, trying to learn PHP.

  37. User Avatar
    Permalink to comment#

    Thanks css-tricks! You are great! You are God!

  38. User Avatar
    Permalink to comment#

    Nice snippet. Can it work whitout mailserver?

  39. User Avatar
    Permalink to comment#

    how to create subscriber form with notification in php ? please help me

  40. User Avatar
    Permalink to comment#

    This not at all working for me. Please help I am getting mad over this!!
    Thanks page is showing but I am not getting mail into my gmail. Do help!!

    • User Avatar
      Rafael Silva
      Permalink to comment#

      Only works on the webmail that your hosting offers.

  41. User Avatar
    Permalink to comment#

    like this function…but i search the same with the thanks page opening in a lightbox.

    Any Help please?

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.