Tuesday, March 30, 2010

E-mail form using HTML and PHP

The first thing to do is create the form using html. We'll call it feedback.html. The form will have an action of send.php, which we'll create later. Below is the html code for our form:


E-mail Form


Name:

E-Mail Address:

Message:







Notice the size="20″ field we added for the name and email fields. This is the character width for the input box. The higher the number the wider the input box. The same applies for the textarea field. Increasing the value of col="30″ will widen the box. Increasing the value of rows="5″ will lengthen the box.

Put these lines in a text file called feedback.html and place the file in the root directory of your web server. After doing this, the form should look something like the image below:

E-mail form using HTML and PHP image 1

Now let's create the script to send the mail. Put the following lines into a text file called send.php and upload it to the root directory of your web server. DO NOT include the numbers or the period at the beginning of each line.
1.
2.
3. Script to send mail
4.
5.
6. Thank you, $_POST[name], Your message has been sent.";
8. //start building the mail string
9. $msg = "Name; $_POST[name] ";
10. $msg .= "E-Mail: $_POST[email] ";
11. $msg .= "Message: $_POST[message] ";
12. $recipient = "you@yourdomain.com";
13. $subject = "Form Submission Results";
14. $mailheaders = "From: My Website ";
15. $mailheaders .= "Reply-to: $_POST[email]";
16. //send the mail
17. mail($recipient, $subject, $msg, $mailheaders);
12. ?>
13.
14.

Lines 9-11 create the $msg variable, a string containing the values typed by the user in the form fields. This is essentially one long message string using the ( ) character to add line breaks where appropriate.

Lines 12 and 13 are the variable naming the recipient and the subject of the email, make sure to put your own e-mail address here and change the subject to whatever you want.

Lines 14 and 15 sets up the: From: and Reply-to: mail headers

Line 17 is the mail( ) function and sends the four parameters: recipient, subject, message and mail headers to your email.

That's it! A very simple e-mail form that can be used over and over.
Download this complete from 

No comments:

Post a Comment