Get Adobe Flash player

Search

Search this site for:


Related Links






Valid XHTML 1.0 Transitional

Valid CSS!





PHP Form Mail Script

A Very simple PHP form processor that goes with the HTML form I created in the HTML forum.
<HTML> <HEAD> <TITLE>Thank You</TITLE> </HEAD> <BODY>
<?php
$date = date ("m/d/Y");
$name=$_POST['name']; $email=$_POST['email']; $comments=$_POST['comments'];
$to="youremail@yourdomain.com"; $subject="The subject for your email"; /* IE: Comments on $date */ $message="Comments from $name\n\nName: $name\nE-mail: $email\nComments: $comments"; $from="$email"; $headers="From: $email\n";
if (mail($to,$subject,$message,$headers)) { echo "<CENTER>Thank you for sending your comments</CENTER>"; } else { echo "There was a problem sending mail, this is a coding issue"; }
?>
</BODY> </HTML>
Lets get go through the explanation of this. You'll notice that I included the HTML tags as it's a good practice to get into. Most of you know how to begin a PHP statement by using <? and ending it with ?>, that's fairly simple.
$date = date ("m/d/Y");
This line configures the date, I enjoy using this and putting it somewhere in the processor, mainly the subject, so I know what date the comments were sent in on. This can be configured many different ways.
$name=$_POST['name']; $email=$_POST['email']; $comments=$_POST['comments'];
These three statements are how you get the information from the HTML form into this PHP processor. This takes from the POST method the HTML form and assigns the output into variables that we can use to produce the output. These output variables can differ depending on the info that your forms provide.
$to="youremail@yourdomain.com"; $subject="The subject for your email"; /* IE: Comments on $date */ $message="Comments from $name\n\nName: $name\nE-mail: $email\nComments: $comments"; $from="$email"; $headers="From: $email\n";
This is all the information to actually output the information. $to is the email address that you wish to send the comments to. $subject is the subject of the email that gets sent. $message is the formatted message that will be the body of the message with the variables, Note: \n skips 1 line. $from is the email address it's coming from, which isn't necessarily needed. $headers creates the headers for the email message, you should make it good attempt to do so, as some mail servers won't accept the mail without headers.
if (mail($to,$subject,$message,$headers)) { echo "<CENTER>Thank you for sending your comments</CENTER>"; } else { echo "There was a problem sending mail, this is a coding issue"; }
This is the piece of code that actually sends the email. PHP has a very simple command to send mail it's mail($to,$subject,$message,$headers); and is very self explanatory, as it mails using those variables. The if statement allows coding error control. If the email address entered isn't correct, or something is missing, it will post an error.
That concludes this PHP form mail tutorial. Please visit http://justincanada.net for more :). Thanks.
About the Author
Justin Robinson http://justincanada.net