Server-side Starter Tutorial:Tell-a-Friend - Part 3 of 6

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):

<?php
$mail_to = $_POST['friend_mail'];
$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.

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  »  

Site Links

 

Starter Tutorials