PHP Email Form Easy

Now you can send emails from your web sites with full control over design style and functionality. PHP is the way to go for online email form. Here is how to get started.
Now you can send emails from your web sites with full control over design style and functionality. PHP is the way to go for online email form. Here is how to get started.

Step 1: Create E-Mail Form

Here is a sample html email form

<form action=”” method=”post”>
Name: <br>
<input type=”text” name=”name”>
Email: <br>
<input type=”text” name=”email”>
Subject: <br>
<input type=”text” name=”subject”>
<textarea name=”message” cols=”” rows=””></textarea><br>
<input type=”submit” name=”submit” value=”Send”>
<input type=”reset” name=”Reset” value=”Reset”>

Copy the html code above and past it into a file with the name contactus.php

Now its time to add the php code.

Step 2: Sending the data

We will first check to see if the form was submitted then we will check to see if all fields were filled in. If all is well, we well send each form data to it’s perspective variables (indicated by the sign “$”) and send out the email with a successful message. If all field were not filled-in, then display an error message. The php code is below.

if ($submit) { // if the form was sent do the following

if($name && $subject && $email && $message ) { // if all fields were filled-in send email
mail(“”,”$subject”,”$message”,”From: $name <$email>”) or die(“email error”);
echo “Message Sent”; // if all went well, display message was sent
} else {
echo “All fields must be filled in!<BR>”; // if not all were filled in, display error message
} // end php submission code

Now just copy and past the php code above into the “contactus.php” file. You can place it anywhere inside the body tag and it will work just fine.

Be sure to check for the next PHP Advanced Email Form coming soon to .

If you like our script, please rate it! has a variety of book on PHP Click here to see the list of PHP books available.

Tagged : / / /

55 thoughts on “PHP Email Form Easy

  1. If your email program has spam filters turned on, you must make sure not to include in your message any words that my trigger the blocking filter. Also make sure your email allows HTML format and that you are sending to the right email account.

  2. I want the email to be confirm so i add a line called email confimation. I would like a warning pop-up to appear if the 2 email adress are not the same, how do i do that ???

    Thank you


  3. First edit the html and add a second email field called “email2” and then change
    the PHP first two lines of code to the following:   

    if ($submit) { // if the form was sent do the following
    if($email != $email2)
    echo "Emails do not match!";
    elseif($name && $subject && $email && $message ) {

  4. What is coding if i want to submit the form to mail, if inside the form got a lot of field. Because when i use this coding, the error message is the argument can not more than 5.

  5. Can you help me on this warning? It also didn’t send mail.

    PHP Notice: Undefined variable: submit in w:\inetpub\wwwroot\test1.php on line 2

  6. I found what is wrong – if I put the PHP code in a new file (form action = “new.php”), then it is ok. Maybe I must do something in PHP.ini, that I don’t get messages on php functions which don’t have initialized variables before clicking submit…?

  7. I have one more question, because I just start learning PHP, I don’t know much… Is there any way to put functions (like that one for sending mail) in one .php file and them calling them from different forms and other php files. What should be then example of calling them… in mail example and if php is in other file then it is action=”otherphpfile.php”, but what if there is more then one function…can I call only one?

  8. If I want to send an html doc to be sent with the email message, how do I do that?
    What I want to do is similar to what eg. does…it allows you to send an article to a friend…but instead I want to send an html email. Is this possible?

  9. I am putting together a second tutorial addressing some of the comments posted below. It should be done by the end of this week.
    – Thanks

  10. Hi! i have a question..when the sender sends the mail there comes a message: “example: thanks will help u soon”. After that, can the form have a code that the sender receives an e-mail back with for example what he has ordered if u are an on line shop???? hope you understand me, i’m from Holland, sorry for my bad English 🙂 Greetings!

  11. Hello, i just would like to be able to send more than 5 parameters because i do need like 17, so it wont let me, is there any way of doing that? arrays? another function? thx!!

  12. Yes you can send email also to sender by adding a second mail() function under the once that is already there. Just make sure to change the “” to “$email”.
    This will also sent a copy of info to the person who submited the form.

    You can edite the “Message Sent” to fit your own needs.

  13. It said

    The requested method POST is not allowed for the URL /test/contactus.html.

    is it my server setup?
    Running Apache 1.3.27

  14. I’ve checked and double-checked everything.
    I’m not receiving the e-mail.
    Though it says “Message sent”.

    If it was being picked up by the spam filter, the message
    would most likely go into the spam folder.

    Any ideas?

  15. Here is a check list:
    • Email address is correct
    • Spam is not blocking your address or message
    • File saved as .php
    • Field names match variable names
    • You have PHP on your web server!!!

  16. When I press Submit, all that happens is, that the form is emptied.
    I have checked, that all input names matches the variables in the script.
    Erik Ginnerskov

  17. Just like to say, thank-you very much. Exactly what I was looking for and worked like a bought one first time.
    No I will just work on the validation of the various fields and I am away.
    Thanks heaps.

  18. The form works great, but when I replace the form button for “submit” with an image-based button the form no longer works.
    This is the code I used for the image button:
    <input name="sumbit" type="image" value="send" src="images/submit.gif">
    Why does this make a difference?

  19. If you change the send button to an image button, you must add a hidden field with the name “submit” and a value “submit” and change the image button name and value to something other then submit. Why? Because the image button can not hold a value.

    Here is the sample code:
    <input name="submit" type="hidden" value="submit">
    <input name="button" type="image" id="button" value="send" src="images.gif">

  20. Thanks for the script, it’s just what I’ve been looking for. I do have one question, though:

    Whenever I tried to add a new field in the form / new variable in the php, I get an “email error” message. How do you go about adding new fields & variables to be displayed in the e-mail I receive? Thanks so much!

  21. Let say you added a extra field named “product” and “Price” and you wanted it to show up in the body of the email above the $message. I would add the following code example above the mail() function in php file:

    $message = “Product = $Product n\Price =$Price \n$message”;

  22. i did as u said.
    Copied the e-mail form;
    pasted the php codings into the body section;
    Error Message on Display page:

    “Notice: Undefined variable: submit in D:\singaporejobsinteractive\contact.php on line 23”

    and line 23 is:
    “if ($submit) { // if the form was sent do the following”

  23. This is the first I have heard of this error. The first thing I would do is view invisible characters and delete all unwanted characters.

  24. i’ve delete all the comments~
    seems that the problem lies with the page unable to recognise the “$submit” value…HOW???

  25. Your php script will only work if you have globals turned on. To work around that problem, you should add the line


    BEFORE the if($submit) line.

  26. Notice that not all handlers can fully understand the $submit as a variable and not as a button. The best way to deal withthis is by having a hidden field in your form

    and replace the if ($submit) with if ($sendme == “ok”)
    this will shut the $submit error up for eternity.

  27. This script is nice and neat as is and works fine for me……. nly problem is that Once sent the screen more or less refreshes itself and gives no indication that the message was succesfuly sent……….

    As we’re not using DB Server Behaviors for this type of form I am wondering how to get round this………… Have you any suggestions as to a script……….. like on post refresh.update..and return a page saying message sent????

  28. instead of diplaying the message sent message, since we’re not using MySql here and we’re not updating server behaviours etc – is it possible to return a new messagesent.php page displaying more detail etc rather that just adding a text string to the existing page???
    If so have you got the code that will do this??? Thyanks

  29. Saint T……….. you’ve lost me here………… advice is a little vage. Could you be a little more specific….Thanks!!!!

  30. Ok John, first thing – do you have the php code in the body of your html page. If you do not, you will not see the php message.

    Second thing – this is a working sample email form using PHP. It works just fine as one page. You don’t need a second message.php page. However, you can use the code and add-on more functions as you wish. Like extracting the php code to a second page call messagesent.php, but make sure your form is pointing to it when it is submitted.

    There are some more email solutions below.

  31. Thanks Alex………..Problem was that I had not set a distinct background colour and therefore the ech statement was ‘invisible’……… I have now corrected that problem but have other issues………….. For example running this sctipt in a Frames environment on clicking the submit button the echo message does not show regardless of what colour I set the page text to……………….

    Furthermore I also get a problem with the $submit variable when testing it on my local server PHP VER 4.3.6 Running on IIS 5.. However when I upload it to the remote server it runs fine without throwing out a variable error message………..Any thoughts on this????

    Have you any sample code you might be able to pots for the messagesent.php page like you suggest…………. I’m currently living in Venezuela and English Books are difficult to come by and expensive…………
    I buy from Amazon about every 3 months and only when I can justify the shipping costs………… so I’m a little stuck for the time being…………….

    Thanks again for the excellent tutorial and not as complicated as some of the others on the site…………. and for making it available for free too! 😉 Hope you can help with the confirm code………

  32. First – The submit problem: Add a hidden field like called “send” just like “Saint Tosin” explained. Then change the php code to check for the “$send” not the “$submit”.

    Second – For a second php page to show the sent status, just copy and past the php code above into a second file called message.php. Go back and change the html form code by adding the page name inside of the form action like (action=”message.php”).

  33. John, i dont know what happened to the sctipt i uploaded earlier, some bits got cut off. This is d deal:

    You have a page named send.php where your form is, all you have to do is include a hidden field in the form with the name ‘send’ and set value to ‘ok’.

    Then on the page you specified in d action part of the form,simply add the line ‘if ($send==”ok”) instead of using if($submit) to initialize your execution.
    check this
    page name = text.php
    [? // code to test if variable sent
    if ($send==”ok”) {
    // do codes here;
    else {
    // i want to use the same form page to process
    // the content of the form below so that if the
    // the form is empty it will show the form and
    // if submitted, it will do d job and hide d form


    [form action=test.php method=post enctype=xxx]
    input type=hidden name=send value=”ok”]
    [input type=text name=wotever size=41 value=””]
    [input type=submit name=”submit” value=”Submit”]

    // end of all these…

    John, this wil help you out if u try it this way.

  34. Plz Help me guys ! I need to make a sender but im a newbie of that ! i downloaded this ! when i put it at my site it dosnt works ! it shows me this when i press send >>>>> Warning: mail() has been disabled for security reasons in /home/ on line 21
    email error <<<<<<< plz help me !

  35. Call your host provided or if its installed on your local computer, you need to edit your config file to allow the php to send mail out using the mail() function.

    If they will not help you try using one of the Host companies listed on this page on the top right under Partners to help support my site. Thanks.

  36. i want to intergrate this mailing system into my main page which is just index.html what do i need to do, to do this! i cant get it to work!

  37. This is a PHP script. Check if PHP is enabled on your host server and change your index.html to index.php and it should work. Call your host provider if you are not sure you can use php on your website.

  38. Took just five mins to get set up…

    Saint Tosin's advice works. Just remember to leave the form's method as POST, but remove the reference of where it needs to post to. (I left it in and it took me fives mins to work out why it wasn't working!)

    Appart from that it worked like a dream, and stops me having to have loads of different and mailform.cgi files lying around when all I need is a simple form2mail application.

  39. Not working, email address is coding out

    Parse error: syntax error, unexpected T_VARIABLE in /home/content/p/address/html/ on line 30

  40. hello.. i have a Form say form.html and i have another php file where php code is stores. name is senform.php… i want that when ever any user clicks on Send button in form.html file.. its result should open in a Lightbox or any PopUp window.. can any body help me,.

Leave a Reply

Your email address will not be published. Required fields are marked *