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:
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.
Related Articles
- PHP Mail Form TutorialThis script is widely used by many sites on the net. PHP provides an easy way to send mail via the function mail().
- Form Mail via CDONTSASP gives you the power to send mail using the CDONTS mail object. This script will send an email from a user to whichever email address you choose...
- How to make a simple form mailer with PHPAs you may be well aware, displaying your e-mail address on your website can lead to e-mail harvesters picking up your address and adding it to hundreds or even thousands of lists used by spammers...
- PHP Contact FormFirst, lets create the html section. I made a simple form, but it gets the job done. You can customize it if you want, but remember to use the same form variables.
- Form Validation using PHPToday Ill take it up a level and re-write that script using the if, else conditional and empty() function to include form validation.
- The Importance of Having a Feedback Form in Your E-commerce WebsiteInternet marketing is a different ball game altogether when compared to traditional marketing. A brick and mortar store employs salespersons to guide and advise its customers. No such equivalent of a salesperson exists for an online store. This could be one of the reasons why people shy away from on...
- Simple Contact FormLearn how to write a clean and easy contact form for your site.
- What is Web Mail?E-mail is something we all get and soon we will not be able to exist without it. In the time we live in today it is becoming more and more important to have a place to be reached on the Internet. At one time, being able to check your E-mail through a client, like Outlook Express, was enough...
- PHP Feedback Form: AdvancedThe problem with my basic PHP Mail Form tutorial is that it offers no protection from spam and meta tag injection. This is fine if you have a very low-traffic site not listed at major search engines, or have a private website, but bigger sites need more.
- Stop SpamPreventing your e-mail that you placed on your site from being found by spammers is very hard. Some have even taken their e-mail addresses off their sites and replaced them with contact forms to prevent this. Here is a neat .htaccess method to help put a stop to this. This will stop many of the bo....
