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”>
<br>
Email: <br>
<input type=”text” name=”email”>
<br>
Subject: <br>
<input type=”text” name=”subject”>
<br>
Text:<br>
<textarea name=”message” cols=”” rows=””></textarea><br>
<input type=”submit” name=”submit” value=”Send”>
<input type=”reset” name=”Reset” value=”Reset”>
</form>

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.

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

if($name && $subject && $email && $message ) { // if all fields were filled-in send email
mail(“you@youraddress.com”,”$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 NewSourceMedia.com .

If you like our script, please rate it!

Amazon.com has a variety of book on PHP Click here to see the list of PHP books available.

55 Responses

  1. EPiK says:

    it says sent, yet the email isnt there! i dunno why it isnt working! i substituted you@youraddress.com for my own.. i dunno why it isnt working.. can you help?

  2. Alex says:

    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.

  3. Philippe says:

    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

    Philippe

  4. Alex says:

    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 ) {

  5. Mark says:

    Yep, no probs, works like a dream!

  6. stellagan says:

    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.

  7. NexGc says:

    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

  8. Alex says:

    What is your code on line two of your test.php file?

  9. NexGc says:

    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…?

  10. NexGc says:

    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?

  11. amy says:

    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. Cnn.com does…it allows you to send an article to a friend…but instead I want to send an html email. Is this possible?

  12. Alex says:

    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

  13. Alex says:

    Amy, see the tutorial called Sending Email with PHP for sending complex emails.

  14. Frontin says:

    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!

  15. Sir_diko says:

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

  16. Alex says:

    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 “you@youraddress.com” 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.

  17. Alex says:

    I am not sure as to what you mean by more then 5 fields. Can you post an example of field you want to use?

  18. Brian says:

    It doesn’t do anything when I hit Submit, all it does is return me to a blank contact page, please advise!

  19. Alex says:

    Can you post a link to the code you are using as a text (.txt) file and also to the php page.

  20. nick says:

    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

  21. Alex says:

    You must change the file name to have the “.php” extension. PHP can not work inside a “.html” file.

  22. Lee says:

    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?

  23. Alex says:

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

  24. Lee says:

    Everything is correct.
    I’m using a host with PHP 4.3.8.

    It still refuses to work.

  25. Erik Ginnerskov says:

    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.
    Yours
    Erik Ginnerskov

  26. AussieClint says:

    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.

  27. Cate says:

    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?

  28. Alex says:

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

  29. Brian says:

    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!

  30. Alex says:

    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”;

  31. Grace says:

    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”

  32. Alex says:

    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.

  33. Grace says:

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

  34. Alan Berman says:

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

    @import_request_variables(\”P\”);

    BEFORE the if($submit) line.

  35. Saint Tosin says:

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

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

  36. John says:

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

  37. John says:

    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

  38. John says:

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

  39. Alex says:

    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.

  40. John says:

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

  41. Alex says:

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

  42. Saint Tosin says:

    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”]
    [/form]

    [?
    }
    // end of all these…
    ?]

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

  43. Alex says:

    If you want to include code tages, replace greater then and less than signs to “[” and “]” brackets.

  44. archeens says:

    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/dta.eclub.lv/public_html/contactus.php on line 21
    email error <<<<<<< plz help me !

  45. Alex says:

    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.

  46. Durk says:

    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!

  47. Alex says:

    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.

  48. woah@lala.com says:

    it took me 1 min to get it to work, SO SIMPLE. thanks!

  49. Chad says:

    Alex you made comment about Advanced Email Form have you had opportunity to work on that?

  50. Uswege Mwaipyana says:

    when i click submit button, only the blak page returns, no message sent nothing happens at all

  51. Dan The Man says:

    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 mailform.pl and mailform.cgi files lying around when all I need is a simple form2mail application.

  52. syabilla says:

    data sucess

  53. syabilla says:

    syabilla comel

  54. Dave says:

    Not working, email address is coding out

    Parse error: syntax error, unexpected T_VARIABLE in /home/content/p/address/html/website.com/contactus.php on line 30

  55. sajid says:

    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