SQL PHP HTML ASP JavaScript articles and free scripts to download
 

PHP tell a friend script to send URL of a page to an email address

We often want to send address or URL of a page to our friend or peers to visit (check) the page. How to send the URL without using any mail program or by going out and preparing an email with the URL? Here is a simple script to do that. You must have seen in many pages in different sites. This script is popularly known as Tell a friend script. This script uses two parts one is to display the html form and the other part is to process the script and sending back the visitor to the original page.

This script uses server side php form validation to check the user inputs and give proper error messages to the user.

If all form validations are passed then the details of the form submitted by the user is posted to the friend's email address using the php mail function.

The URL of the page is dynamically collected by using the php HTTP_REFERER function which collects the referrer of the page. This is done to maintain the portability of the script. This way the code can be used in any page or the form can be placed in any page and one common form processing page can be used to handle forms used in different pages. In such a case the form submit path is to be modify to take care the directory levels of the page relative to the main processing script.
Here is the code for the form. You can use the form shown at the end of this page to send this page url to your friend. Check once ...

<form method=post action=tellck.php>
<table border="0" cellpadding="0" cellspacing="0" align=center>
<tr bgcolor='#f1f1f1'><td colspan=2 align=center><font face='Verdana' size='2' ><b>TELL A FRIEND</b></font></td></tr>
<tr><td width=100><font face='Verdana' size='2' >Your Name</font></td><td width=200><input type=text name=y_name></td></tr>
<tr bgcolor='#f1f1f1'><td><font face='Verdana' size='2' >Your Email</font></td><td><input type=text name=y_email></td></tr>
<tr ><td><font face='Verdana' size='2' >Your Friend's Name</font></td><td><input type=text name=f_name></td></tr>
<tr bgcolor='#f1f1f1'><td><font face='Verdana' size='2' >Friend's Email</font></td><td><input type=text name=f_email></td></tr>
<tr ><td><font face='Verdana' size='2' >Your Message</font></td><td><textarea name=y_msg rows=5 cols=20></textarea></td></tr>
<tr bgcolor='#f1f1f1'><td colspan=2 align=center><font face='Verdana' size='2' ><input type=submit value='Send'></font></td></tr>
</table>
</form>


You can see this from submits the details to tellck.php page. Here is the code to post the details to friends email address. Use the form at the end of this page to test the script.

The script is modified to take care of register_global if it is off and one check is added to prevent spammers using this form to keep more than one email address in the email fields. The script also checks for any URL entered by users , this is not allowed as spamers will use this to send links.

You can keep the textarea of the form as not editable and keep a standard message. This will prevent spamers from using it to send their messages. Here it is kept as fixed message and a fixed string for $y_msg variable. You can change this as per your requirement.

Script is modified to add better email validation

The script is further modified to add IP address of the visitor at the form and at the end of the outgoing email.



<?
$status = "OK";
$msg="";

$y_email=$_POST['y_email'];
$y_name=$_POST['y_name'];
$f_email=$_POST['f_email'];
$f_name=$_POST['f_name'];
$y_msg=$_POST['y_msg'];


if(substr_count($y_email,"@") > 1 or substr_count($f_email,"@") > 1){
$msg .="Use only one email address<BR>";
$status= "NOTOK";
}

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $y_email)){ // checking your email
$msg .="Your email address is not correct<BR>";
$status= "NOTOK";}
Related Tutorial
PHP Mail
PHP GuestBook


if (strlen($y_name) <2 ) { // checking your name
$msg .="Please enter your name<BR>";
$status= "NOTOK";}

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $f_email)) { // checking friends email
$msg .="Your Friends address is not correct<BR>";
$status= "NOTOK";}

if (strlen($f_name) <2 ) { // checking freinds name
$msg .="Please enter your friend's name<BR>";
$status= "NOTOK";}

if (strlen($y_msg) <2 ) { // checking Message details
$msg .="Please enter your message details<BR>";
$status= "NOTOK";}

if(stristr($y_msg,"http://") or stristr($y_msg,"https://") or stristr($y_msg,"<a href") or stristr($y_msg,"http://") or stristr($y_msg,"[url=")){
$msg=$msg."Links or URLs are not allowed <BR>";
$status= "NOTOK";}


if($status=="OK"){ // all validation passed
/////////// Sending the message starts here //////////////
$ref=@$HTTP_REFERER;
/////Message at the top of the page showing the url////
$header_message = "Hi $f_name \n Your friend $y_name requested you to visit the page at \n $ref \n";
/// Body message prepared with the message entered by the user ////
$body_message =$header_message."\n".$y_msg."\n";
$body_message .="\n This is a free script from http://www.plus2net.com";
//// Mail posting part starts here /////////
$headers="";
//$headers = "Content-Type: text/html; charset=iso-8859-1\n".$headers;
// Un comment the above line to send mail in html format
$headers4=$y_email; // Change this to change from address
$headers.="Reply-to: $headers4\n";
$headers .= "From: $headers4\n";
$headers .= "Errors-to: $headers4\n";
$subject="Request to visit URL";
mail($f_email,$subject,$body_message,$headers);
////// Mail posting ends here ///////////
echo "<center><font face='Verdana' size='2' color=green>Thank You, Your message posted to $f_name</font></center>";
//////////// Sending the message ends here /////////////


}else{// display the error message
echo "<center><font face='Verdana' size='2' color=red>$msg</font></center>";
}
?>


To send BCC you can add these modifications
Modify the header part to add
$headers .= "bcc:siteadmin@sitename.com, admin@sitename.com\n";
Or you can use one more line of mail function with site admin address.

Download the ZIP file here for tell a friend script

Use this form below to post the url of this page to your friend

Your IP address=38.103.63.18

For a short period we have stopped the demo of this script. You can download and try at your end.

TELL A FRIEND
Your Name
Your Email
Your Friend's Name
Friend's Email
Your Message
(Editing of text area is disabled. You can use it after downloading the script)

Discuss this tutorial at forum


 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.