|
|
Jul 30, PST
|
|
Creating the Base of your Form -by Thomas A PHP e-mail form is very helpful in many ways. You can let your visitors easily contact you. It's also much safer than posting your e-mail on the internet in case a spam bot picks it up. To get started you need to create a new page where you want the form to display. I made mine "contact.php" Paste the following code on contact.php <form method="post" action="contact2.php"> Name: <input name="name" type="text"><br> Email: <input name="email" type="text"><br> Message:<br> <textarea name="message" rows="5" cols="30"></textarea><br> <input type="submit" value="Submit Form"><input type="reset" value="Clear Form" name="Clear"> </form> Next you need to make another page. This page must end with ".php". I made mine contact2.php. This page will process through the information sent by contact.php and send the email. NOTE: If you decide to change the name of this page make sure you change this code on contact.php: action="URL OF NEW PAGE" On the page you just made put the following code. <?php $to = "EMAIL HERE"; //put the email you want the messages sent to where is says email here $subject = "SUBJECT HERE"; //put the subject there $name = $_POST['name']; $email = $_POST['email']; $message = "IP: ".$_SERVER["REMOTE_ADDR"]." Message: ".$_POST['message']; $headers = "From: $name <$email>"; $sent = mail($to, $subject, $message, $headers); if($sent) {echo "Your message was successfully sent!"; } //message if email was sent else {echo "Your message was not sent. Please try again."; } //message if email was not sent ?> On the second line put the e-mail you wish the form to be sent to where it says EMAIL HERE. On the third line put the subject of the message. You don't need to change anything until the 10th line. Put the message you wish will display if the message was sent. And the last line to edit is line 12. Put the message you want it to say if the message was not sent. You can use HTML but make sure you put a backslash (\) before any double quotes ("). Example: <font color=\"red\"><b>Sorry, your message was not sent. Please try again.</b></font> If you have enough HTML and PHP knowledge you can probably edit the above code's and add more fields, options, etc. Feel free to customize it to meet your needs. Adding Security to your Form -by Haily Now we're going to add some security options to the form to prevent an email from being sent when the user inputs invalid information. Check for a Valid Email First we'll add to contact2.php so that we can confirm that the information entered into the email field is a valid address. Add this to the top of the file: if(!preg_match("/^[\.A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])){ die("<b>Error!</b> The email address you entered is not a valid email address."); } Check for a Valid Message To prevent spam we also need to check that whatever was entered as the message isn't spam or empty. For this, we will count how many characters are in the submitted message (excluding any whitespace). You can add this also to the top of contact2.php. if(strlen(trim($_POST['message'])) < '10'){ die("<b>Error!</b> The message you submitted is too short. Please do not submit spam."); } Adding a Captcha Adding an image confirmation to your web form is easier than you think. Sure, it's complicated to configure your web server to support one of the various options out there for an image captcha. But there a perfect and FREE substitute to to that called reCaptcha. I won't cover how to install reCaptcha to your form mainly because they do it better on their website. All you need to know is the difference between the two files: contact.php (where the form is) and contact2.php (where the form is processed) Try it: http://recaptcha.net/ The Final Code That's it! Here's what you should have (without the Captcha). contact.php <form method="post" action="contact2.php"> Name: <input name="name" type="text"><br> Email: <input name="email" type="text"><br> Message:<br> <textarea name="message" rows="5" cols="30"></textarea><br> <input type="submit" value="Submit Form"><input type="reset" value="Clear Form" name="Clear"> </form> contact2.php <?php
if(!preg_match("/^[\.A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])){ die("<b>Error!</b> The email address you entered is not a valid email address."); } if(strlen(trim($_POST['message'])) < '10'){ die("<b>Error!</b> The message you submitted is too short. Please do not submit spam."); } $to = "EMAIL HERE"; //put the email you want the messages sent to where is says email here $subject = "SUBJECT HERE"; //put the subject there $name = $_POST['name']; $email = $_POST['email']; $message = "IP: ".$_SERVER["REMOTE_ADDR"]." Message: ".$_POST['message']; $headers = "From: $name <$email>"; $sent = mail($to, $subject, $message, $headers); if($sent) {echo "Your message was successfully sent!"; } //message if email was sent else {echo "Your message was not sent. Please try again."; } //message if email was not sent ?>
| Written by: Thomas, Haily | Added: Sep 25 2009 | Last Modified: May 18 2010 | Views: 2,162 | Member Comments
| |||||||||||||||||||||||||||||||
|
Neo Crave | Enchant Me Not | FGN-Guild.com | Rabidish | Unloadeed | Blue Stapler | Moonwalked.net | RockyRoadRules | KeliJo.net | Daily Neopets | RainbowBliss | Tugboat | Guildpets | NeoWishes | Neo Nutters | Neoeditor | Gamexe.net | Dash of Color | Figmint | Hearted Kind | Darkgirl's Life | Neo-Richies | Geeks' Planet | Skyline Designs |
|||||||||||||||||||||||||||||||