Adding Tell a Friend to a Web Page

Adding a "Tell a Friend" option to a web page isn't all that difficult if the web page supports PHP. We need two pieces of code - a form for the person wanting to tell their friend to enter their email address and that of their friend in order for the email to go to the right person from the right person, and a script to actually validate those fields and send the email.

Obviously you have your own ideas as to how the form on the page should look and what the email that is sent should contain and so I am not going to get at all fancy with either the form or the email content. The easiest way to get the form displaying the way you want is to take the code that I supply and style it using CSS. For the email being sent I'll just include a short sentence that suggests to the recipient that you have found a web page that interests them and gives the address of the current page. You can substitute whatever content you like for that and just substitute the $page variable where you want the web page address to appear. You can even add extra fields if you like to collect the names of the sender and intended recipient, it should be relatively obvious from the code where you need to add the extra field references and vaslidations.

We'll start with the form for them to enter their email addresses in (along with a little PHP to display an error message if one or both email addresses is obviously invalid.

<form method="post" action="<?php echo $PHP_SELF ?>">
<legend>Tell a Friend</legend>
<?php if ($error != '') echo "<p>$error</p>"; ?>
<input type="hidden" name="page" value="<?php echo thispage(); ?>" />
<div><label for="yem">Your Email Address</label>
<input type="text" name="yem" id="yem" /></div>
<div><label for="fem">Friend's Email Address</label>
<input type="text" name="fem" id="fem" /></div>
<input type="submit" value="Tell Your Friend" />

Of course we also need the PHP code to put in the top of your web page that sends the email if the form was submitted.

This code includes a function to build the full web address of the current web page that is called by the hidden field in the form (this saves your having to hard code the page addresses). It reads in the posted variables rejecting any that are not in the $allowed array so that it will only accept the fields that are actually in our form (so you'll need to add your field name sto the array if you add more fields to the form). It validates that the email addresses at least look like email addresses (although this validation will reject email addresses containing spaces and email addresses with an ip address instead of a domain but then those are not common email formats even though they are valid). It then uses the mail() command to send the email.

Note that several of the lines in the script are too long for the display and I have wrapped them onto a new line in the code as indicated by the arrow on the left of the continuation. These should go on the end of the preceding line for the code to work.

function thispage() {
$p = 'http';
if ($_SERVER["HTTPS"] == "on") {$p .= "s";}
$p .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
} else {
return $p;
$page = $yem = $fem = $error = '';
$allowed = array('page','yem','fem');
reset ($_POST);
while (list ($key, $val) = each ($_POST))
if (in_array($key,$allowed) && $val)
$$key = $val;
if ($page != '') {
if (preg_match('/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|
continued from previous line(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/' , $yem) == 0)
$error .= "$yem is not in a valid email address<br />";
if (preg_match('/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|
continued from previous line(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/' , $fem) == 0)
$error .= "$fem is not in a valid email address<br />";
if ($error == '') {
mail("$fem","A web page worth visiting","Just a short email to tell you about this web page that I think
continued from previous lineyou might be interested in - $page","From: $yem");


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow