Llamasery.com Home  
 
Server-side Starter Tutorial: - Part 3 of 6
Alfie the alpha_geek: Llamasery guru-in-waiting Tutorials Purpose

Starter Tutorials

Tell-a-Friend
Tell-a-Friend +
The 'Contact Form'
php Includes
Simple Themes
MySQL database 101
Comments System
Text Files

Intermediate Material

Articles/Tutorials

Sending Mail

This is another easy bit, since php has a built in mail function. The syntax is:

mail($From, $To, $Subject, $MailBody)

OK, let's see the script

Putting it all together, here's the tell-a-friend script (taf.php):

<?
$mail_to = $_POST['friend_email'];
$mail_from = "From: webmaster@yourdomain.tld";
$mail_subject = "Site recommendation";
$mail_body = "A friend recommended http://www.yourdomain.tld";
mail("$mail_to","$mail_subject","$mail_body","$mail_from");
?>

Save that little piece of code as taf.php. Upload taf.php and taf.html (your form) to some testing space on your web server, and navigate to taf.html. Enter your own e-mail address as the 'friend' and sit back. Check you e-mail and you should have received a message recommending a web site. You have successfully implemented your first useful php script!

If not, check your code very, very carefully for spelling, spacing, punctuation, etc. And so should I have done. A keen-eyed visitor got this script working even though there was a typo in the original article!! Thanks, Kelso, and it's fixed now.

There are, of course, a zillion improvements we can make to the form and the script - getting more information, doing more with the information, error checking and such. So, basking in the glow of success you can either stop right now, or carry on with this tutorial to learn a little more.

«  previous  |  next  »