PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP E mail form using html and php

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:

<html>
<head>
<title>E-mail Form</title>
</head>
<body>
<form action="send.php" method="POST">
<p>Name:<br /> <input type="text" size="20″ name="name" /></p>
<p>E-Mail Address:<br /> <input type="text" size="20″ name-"email" /></p>
<p>Message:<br />
<textarea name="message" cols="30″ rows="5″></textarea></p>
<p><input type="submit" value="send" /></p>
</form>
</body>
</html>

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

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. <html>
2. <head>
3. <title>Script to send mail</title>
4. </head>
5. <body>
6. <?php
7. echo "<p>Thank you, <b>$_POST[name]</b>, Your message has been sent.</p>";
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 <you@yourdomain.com> ";
15. $mailheaders .= "Reply-to: $_POST[email]";
16. //send the mail
17. mail($recipient, $subject, $msg, $mailheaders);
12. ?>
13. </body>
14. </html>

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.



Author's URL: 2torial Blog
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Reader's comments
comments sulanum February 12, 2012 says:
faill script........
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:xampphtdocsmatthiassend.php on line 17

Reply
comments web design September 22, 2011 says:
How to make Thai language encoding?

www.webdesign.co.th

Reply
comments pntdondoc September 21, 2011 says:
What is this?
"Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:xampphtdocshocphpsend.php on line 18"
I can't run. Please, help me!

Reply
comments anand July 15, 2011 says:
advisor@bulljobs.com.. send me the explanation
Reply
comments anand July 15, 2011 says:
Works Great. just one problem ... its not sending mail.. y is it
Reply
comments Fred February 28, 2011 says:
In response to the comments: this tutorial is only useful if the PHP server has STMP setup. Without the STMP configurations, the email WILL NOT be sent from your website to your inbox. To learn how to fix this, google "how to setup stmp in php"
Reply
comments Islam mohiy January 15, 2011 says:
thanks but it don't work it gives me the message "Thank you Your message has been sent."
and when i go to my e-mail i don't find any messages. i wanna to tell you that i had done all you say,is i have to give any port number or else..

Reply
comments Puja kumari December 29, 2010 says:
thanks but it don't work it gives me the message "Thank you Your message has been sent."
and when i go to my e-mail i don't find any messages. i wanna to tell you that i had done all you say,is i have to give any port number or else..

Reply
comments ranjit December 03, 2010 says:
very nice explanation in code
Reply
comments fady April 03, 2010 says:
hi thanks but it don't work on me ::: it gives me the message "Thank you Your message has been sent."
and when i go to my e-mail i don't find any messages. i wanna to tell you that i had done all you say

Reply
comments charan March 25, 2010 says:
VERY EXLLENT
Reply
comments Sourabh Patrikar February 12, 2010 says:
It's Truly good and best for use.thanks for launch this type of tutorials.
Reply
Add comments to "E mail form using html and php"

Captcha