Latest tutorial: Premium Flash Files | Ask Tutorial5! | Subscribe to RSS Register Login Find Hobbies
Advertisement
Do you need more help? You can now Ask Tutorial5! and get free support - Ask a question now!

Mail Form with PHP and HTML

(13 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_text69631 ); 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.

Do you need more help? Ask now!
 

busy
Last Updated ( Sunday, 14 October 2007 )