If you’re like most web site owners you’re always on the lookout for a bit of free marketing. What sales and marketing folks have known if years is that nothing is more effective than a referral from a friend or relative. In the web world this is concept is sometimes referred to as “viral-marketing”. The idea is that if a percentage of your web sites visitors tell one or more friends about your site your almost guaranteed to get more targeted traffic.

With this simple PHP script, adding the tell-a-friend function is a breeze.

First you’ll need to create a form entry HTML page (e.g. “tellafreind.htm”) where the visitor can enter their information and friends information, then place a link on your front page referring them to this page on your site. Place the following HTML in the <body> section of your newly created tell-a-friend page.

<p align=center><strong>Tell a friend about MYSITE</strong></p>
<form method=”POST” action=”sendtofriend.php”> </p>
<p>Your Name: <input type=text name=”visitor” size=”30″></p>
<p> Your Email: <input type=text name=”visitormail” size=”30″></p>
<p> Friend’s Name: <input type=text name=”friend” size=”30″></p>
<p> Friend’s Email: <input type=text name=”friendmail” size=”30″></p>
<p> Message:<br>
<textarea name=notes rows=4 cols=80></textarea></p>
<p> <input type=submit VALUE=”Send Message”></p>
</form>

Now create a second send page “sendtofreind.php” with the following PHP code in the body section that gets called when the form is submitted.

<?php
$myemail = "webmaster@mysite.com";
$sitename = “MySite Title”;
$urlis =”http://www.mysite.com”;
$redirectlink = “http://www.mysite.com”;
?>
Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> ) <br>
For sending : <a href=”<?php echo $urlis ?>”> <?php echo $sitename ?>
</a> (link)<br>
To: <?php echo $friend ?> (<?php echo $friendmail ?>)
<a href=”<?php echo $redirectlink ?>”> Return to MySite Home Page </a>
<br>
<?php
$ip = getenv(”REMOTE_ADDR”);
if (!isset($visitormail) || !isset($friendmail))
echo “Please go back and input valid e-mail addresses. Thanks.</font> $ip” ;
$todayis = gmdate(”l, F j, Y, g:i a”) ;
$notes = stripcslashes($notes);
$messageis = “$todayis [GMT] \n” .
“To: $friend ($friendmail) \n” .
“Your Friend: $visitor ($visitormail) \n” .
“Recommended you visit the MySite web site ($urlis). \n” .
“Comments: $notes \n\n\n\n\n” .
“This e-mail was sent using the tell-a-friend feature at www.mysite.com ” .
“from a user at IP address: $ip\r\n” .
“If this e-mail was sent inappropriately, please contact webmaster@mysite.com\r\n”;

if ($csMail != "")
mail($csMail, $sitename, $messageis);
if ($friendmail != “”)
mail($friendmail, $sitename, $messageis);
if ($visitormail != “”)
mail($visitormail, $sitename, $messageis);
?>

Replace all occurrences of “mysite” with your site name.

Note, if you cut and paste this code your need to paste it into NotePad (or other plain text editor) first so you don’t pick up the extra HTML tags from IE.

PS. If you like this code sample, please link to our site (http://www.webmastersloom.com). It will be much appreciated.