Email Forms In PHP, The Easiest Yet...
By Dan Ball
2003-09-27

Compiling And Sending The Email
Look at the else in the final if statement:
else
{
$message = "Name: $Name\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $Name\r\nReply-To: $Email\r\n";
mail ("noone@nowhere.com", "Website Email", $message, $extra);
echo "<p class=bodymd>Thanks for your inquiry, $Name.</p>";
echo "<p class=bodymd>A response will be sent to $Email as soon as
possible.</p>";
}
The first line sets the body of the email, this is where it takes the values of all the fields, which are variabilized with the field name preceded by a "$", so the field name "Name" is known to PHP as "$Name". The \n, as most of you I am sure know, starts the text on the next line. Therefore, $message = "Name: $Name\nEmail: $Email\nComments: $Comments\n"; will result in this message body:
Name: {value of Name field}
Email: {value of Email field}
Comments: {value of Comments field}
The second line is not necessary to send email, but it is cool, as it sets the "from" and "reply-to" fields in your email client. It's a neat feature.
Now sending it, you won't believe how easy this is...all it takes is:
mail ("noone@nowhere.com", "Website Email", $message, $extra); the "mail" statement sends the defined values to the sendmail program configured in you web server. The first value is where the email gets sent to, the second is the subject of the email, the third is the body that you complied above, and the last is the "from" and "reply-to" statement from above...thats it! can you believe that!
The statements following that are simply printing a "thank you" statement to the browser to tell the visitor that the email was successfully sent.
Now how easy was that!!!
If you found this article interesting, you may want to read these as well:
» Protecting your PHP and HTML Source Code
» Publishing Newsletters Using PHP & MySQL - 4
» Publishing Newsletters Using PHP & MySQL - 3
» Publishing Newsletter Using PHP & MySQL - 2
» Publishing Newsletters Using PHP & MySQL
» Unix Webserver Crontab Basics
|