Latest tutorial: Making a Movieclip face another Movieclip or point on the stage | Ask Tutorial5!
 

Mail Form with PHP and HTML

(16 votes)
Written by DanielRo   
Before starting you should check that your host has PHP support with the mail() function activated.
For this you need to create a php file, name it test.php and enter the following code:

<? mail(" This e-mail address is being protected from spam bots, you need JavaScript enabled to view it ","test message","This is a test"); ?>

Some hosts take a little longer to send the message, thus you should wait a while for the e-mail to arrive in your inbox.
If you do not recieve the test e-mail you should contact your Hosting Provider, otherwise read further.

The HTML FORM

Fireup your favorite text-editor, create a new file and paste in the following HTML code.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML Mail Form</title>
</head>
<body>
<form action="send_mail.php" method="post">
Name: <input type="text" name="name" size="30" /><br />
Email: <input type="text" name="email" size="30"/><br />
Subject: <input type="text" name="subject" size="30"/><br />
Text:<textarea name="text" name="text" cols="50" rows="10"></textarea><br />
<input type="submit" name="submit" value="Send" />
</form>
</body>
</html>

Save it As: contact_us.html
Now for the code hints.

In this line you will notice that i introduced send_mail.php that is the name of the PHP file that will process the data sent from the HTML file. The method we use for this form is POST. There are two methods that can be used POST and GET.

<form action="send_mail.php" method="post">

In this part of the script we have our general Input Fields were the user introduces it's data (name, email, subject and mail-text). The "name" values that you notice next on every line are required by the PHP script file that is going to process the mail, and should be named carefully.

Name: <input type="text" name="name" size="30" /><br />
Email: <input type="text" name="email" size="30"/><br />
Subject: <input type="text" name="subject" size="30"/><br />
Text:<textarea name="text" name="text" cols="50" rows="10"></textarea><br />

This line generates a Button that will submit the form data to the PHP script file.

<input type="submit" name="submit" value="Send" />

The PHP script file - processing the data

Now that we created our HTML form we need to script the PHP file that will process the sent data. Remember the <form action="send_mail.php" method="post">, we now need to create that file.
Create a new file and insert the following code:

<?php
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$text = stripslashes($text);
mail(' This e-mail address is being protected from spam bots, you need JavaScript enabled to view it ',$subject,$text,"From: $name <$email>");
echo("Thank you for your interest, your e-mail was sent.");
?>

Save it As: send_mail.php
Now for the code hints.

Remember that we used POST in our HTML form when sending data to the PHP script. Now using the extract function we will recieve the POST data sent and PHP will translate it into single-word variables.

@extract($_POST);

In the next lines we use the stripslah function to remove any non-alfanumeric characters from the user inputed data, thus making it user friendly.

$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$text = stripslashes($text);

Now by using the mail() function, we are putting it all together. The mail() function collects all the data and sends it to the e-mail address you specify.

mail(' This e-mail address is being protected from spam bots, you need JavaScript enabled to view it ',$subject,$text,"From: $name <$email>");

After the users hits the Submit button, and the mail is sent, this message will be shown on the page.

echo("Thank you for your interest, your e-mail was sent.");

You can also download the php e-mail form files.
This is just a basic example of the mail() function in php, for more info visit the official php.net website.



Subscribe now via RSS feed and get all the new tutorials

written by Waseem Hassan , August 08, 2007

This is nice tutorial for sending email with php
written by Luke , September 22, 2007

Gdgd (y)
written by nikki , November 28, 2007

I WANT TO SEND EMAIL FROM LOCALHOST IN PHP CAN I DO THIS..
written by HNX , December 04, 2007

is this the same as above?
written by HNX , December 05, 2007

i need somebody to explain how it works
written by Thushari , January 10, 2008

how to send a html body in php mail form
written by Himani , January 23, 2008

i tried it but some internal server error occurred.. can u tel me how to resolve it?
written by DanielRo , January 23, 2008

Hello,
Please post what php error you are receiving.
written by liezel , February 27, 2008

Good day,
i am a new user of php and mysql. Now i start to develop website for our school IGP(Income generating project) which features clients registration, product ordering and hostel reservation..my problem now is how to insert mail() function on the system which enables the system to send mail to the users email whenever they register,order, reserve hostel rooms or anytime they will performed any transaction on the igp system.I already had the registration,ordering and reservation but the problem is email function.
pls give me idea how mail function on this system will be implemented?

Thank you!

written by thiyagarajan , March 17, 2008

i tryed by using your coding to sent message to my mail but it is not working.

i changed this code:
mail(' This e-mail address is being protected from spam bots, you need JavaScript enabled to view it ''; document.write( '' ); document.write( addy_text53404 ); document.write( '<\/a>' ); //-->\n This e-mail address is being protected from spam bots, you need JavaScript enabled to view it ,$subject,$text,"From: $name ");
as
mail(' This e-mail address is being protected from spam bots, you need JavaScript enabled to view it ',$subject,$text,"From: $name ");
but it is not working. what is the problem?
written by AW , March 24, 2008

You spelt 'stripslash' wrong. (stripslah)

You might want to change your tutorial's name; it is misleading and makes the viewer think you deal with HTML being send with your email.
written by thiyagarajan , March 25, 2008

THANK YOU,YOUR MAIL CODING IS NOW WORKING for me.
written by rob , June 17, 2008

thank you for the code it was a great help..
written by P. , July 01, 2008

How do I create a mail form giving the user the ability to select different emails preferably through a dropdown box?
written by krshna , July 04, 2008

Hi,

Parse error: syntax error, unexpected T_STRING in /homepages/3/d246704272/htdocs/dsc049447899/send_mail_01.php on line 5

this is the error I got in my website. Can I chat with you offline here.

Will post my code.


written by Nitro , July 14, 2008

hello,
I`m a new user also on Php and Html. So i was training to send an e-mail from my website that i created on my localhost so could you look up code at the below where i have got mistake?


i have got error on the "mail($to, $name, $email, $text, $headers);"
Parse error: parse error, unexpected T_STRING in C:......

Thank you,
written by yogesh kandwal , August 06, 2008

Thank you.

for this site because i get help from see it's.
written by wow gold , September 04, 2008

I`m a new user also on Php and Html. So i was training to send an e-mail from my website that i created on my localhost so could you look up code at the below where i have got mistake?
written by wow gold , September 04, 2008

for this site because i get help from see it's.
written by wow powerleveling , October 11, 2008

So i was training to send an e-mail from my website that i created on my localhost so could you look up code at the below where i have got mistake?
written by wow powerleveling , October 11, 2008

I`m a new user also on Php and Html. So i was training to send an e-mail from my website that i created on my localhost so could you look up code at the below where i have got mistake?
written by Kelly Sly , October 30, 2008

I have tested the PHP mail function and it is working. I downloaded the contact_us.html and send_mail.php files and changed the email address in the script. It says that it sent, but I'm not getting an email from it. The mail error log says the following (From address not in member domain. Message not sent.) Please help!!

http://www.yardrat.net/contact_us.html

Do you need more help? Ask now!
 

busy
Last Updated ( Sunday, 14 October 2007 )