PHP/MySQL How to Create and use Tell a Friend Script in PHP
How to Create and use Tell a Friend Script in PHP
This Script for Tell a Friend featuring
· Page tracking for landing page
· PHP Validation
· Ability to send to muliple friends at once
· User Email/Message optional or required
This PHP script consists of 2 files
1. config.php
2. tell.php
Where config.php can be used for Site Options and configuration. Open config.php and edit it to your specification, use notepad/wordpad/frontpage/dreamweaver etc. to edit it.
Setup Options:
Require Email -> Require user to enter their email to send a TAF (Tell-a-Friend) message, 1= yes, 2=no
Require Message -> Require user to enter a message to send a TAF (Tell-a-Friend) message, 1= yes, 2=no
Multiple Emails -> Allow users to send multiple emails (separated by Comma) to friends.
Current Landing Page -> If enabled, message will include the current page to which the user sent the TAF from.
Site Title -> Title of the site, e.g.: allfree-stuff.com All Free Stuff for you
Site URL -> URL of the site, e.g.: http://www.allfree-stuff.com
Site Email -> To be used for Reply to and From headers
Email Charset -> UTF-8 should suit most, but you can localize it if needed
Email Type -> text/plain (text only) or text/html (html+text)
Email Subject -> Subject of Email sent
Email Template -> Email to be sent to user
To Use this Script in your website
To use this script, place this code andwhere where you want the counter to appear:
Create the following 2 files and upload them in your website.
CONFIG.PHP
//Site Options and configuration
//Require user to enter their email -> 1=yes,2=no
$REQUIRE_EMAIL = 1;
//Require user to enter a message -> 1=yes,2=no
$REQUIRE_MESS = 1;
//Allow Multiple emails be sent -> 1=yes,2=no
//Multiple emails separated comma
$MULTIPLE_EMAILS = 1;
//Send current page in email -> 1=yes,2=no
$CUR_PAGE = 1;
//Site title
$EMAIL_OPTIONS['TITLE'] = 'Demo of Tell A Friend PHP Script ' ;
//Site URL
$EMAIL_OPTIONS['URL'] = 'http://allfree-stuff.com';
//Main email
$EMAIL_OPTIONS['FROM'] = 'webmaster@allfree-stuff.com';
//Charset
$EMAIL_OPTIONS['CHARSET'] = 'utf-8';
//Type -> HTML=text/html Text = text/plain
$EMAIL_OPTIONS['TYPE'] = 'text/html';
//Email subjects
$EMAIL_OPTIONS['EMAIL_SUBJECT'] = 'Tell A Friend For You' ;
/*
For Templates you can use the following variables:
$+name+$ -> User Name Sending Contact
$+page_send+$ -> Page user is comming from
$+message_text+$ -> Contact Message
*/
//EMAIL Template
$EMAIL_TEMPLATE = 'Hello,
Your friend $+name+$ decided to sent you a tell-a-friend notice.';
if($CUR_PAGE == 1){
$EMAIL_TEMPLATE .= 'You can visit the page your friend sent his email: $+page_send+$';
}
if($REQUIRE_MESS == 1){
$EMAIL_TEMPLATE .= 'Your friend\'s message was: $+message_text+$';
}
$EMAIL_TEMPLATE .= 'You can visit our main url @ '.$EMAIL_OPTIONS['URL'].',
'.$EMAIL_OPTIONS['TITLE'].'';
/* +++++++++++++++++++++++++++++++++++++
END FILE CONFIGURATION
---------------------------------------*/
?>
TELL.PHP
//Include configuration file and function file
//(default location is in the same directory)
include_once('config.php') ;
//Function to check for valid email
function is_valid_email($string) {
return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $string) ;
}
//Check cur page
if($_POST['this_page'] == NULL){$_POST['this_page'] = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']; }
//If contact is being sent:
if($_POST['submit_id'] == 1){
//Check name entered
if($_POST['name'] == NULL){ $message = 'Please enter your name.';}
//check if email is enetered
if($message == NULL && $REQUIRE_EMAIL == 1 && is_valid_email($_POST['email']) == false ){ $message = 'Please enter a valid email.';}
//check if message is entered
if($message == NULL && $REQUIRE_MESS == 1 && $_POST['message_text'] == NULL){ $message = 'Please enter a comment.';}
//check to send emails
if($message == NULL)
{
if($MULTIPLE_EMAILS == 1){
//multiple emails
$valid_emails = array();
$emails = explode(',',$_POST['toemails']);
foreach($emails as $email){
$email = trim($email);
if($email != NULL){
if(is_valid_email($email) == false ){
$temp_message .= 'We couldn\'t send and email to: '.$email.' because it is not valid.';
} else {
array_push($valid_emails,$email);
}
}
}
if(count($valid_emails) <=0 ){
$message = 'Please enter at least one email to send message to.';
}
} else {
//one email
if(is_valid_email($_POST['toemails']) == false ){ $message = 'Please enter a valid friend\'s email.';}
}
}
//End verifications, start processing
if($message == NULL){
//compose user message templates replaces
$do_search = array('$+name+$','$+page_send+$','$+message_text+$');
$do_replace = array($_POST['name'],$_POST['this_page'],$_POST['message_text']);
$subject = str_replace($do_search,$do_replace,$EMAIL_TEMPLATE);
//Set Headers
$headers = "Return-Path: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
$headers .= "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n";
$headers .= "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=".$EMAIL_OPTIONS['CHARSET'].";\n\n\r\n";
if($MULTIPLE_EMAILS == 1){
foreach($valid_emails as $this_email){
// mail ($this_email,$EMAIL_OPTIONS['EMAIL_SUBJECT'],$subject,$headers);
$one_pass = true ;
}
} else {
mail ($_POST['toemails'],$EMAIL_OPTIONS['EMAIL_SUBJECT'],$subject,$headers);
}
$message = 'Your emails have been sent, thank you.';
$message .= $temp_message ;
$_POST = NULL ;
}
}
if($message != NULL){
?>
" method=post>
Your name:
" name=name>
if($REQUIRE_EMAIL == 1){?>
Your Email:
" name=email>
Your Friend's emails:
Separated by comma
Your Friend's email:
" name=toemails>
if($REQUIRE_MESS == 1){?>
Message:
" name=this_page>
No comments:
Post a Comment