<?php
//PHP Script: Tell A Friend : Copyright Christian-web-masters.com
//This version uses a PHP class called PHPMailer rather than PHP default mail function.
//You can get the default mail function version at:
// http://www.christian-web-masters.com/articles/web_free-php-tell-scripts.html
// That version requires no extra files to download.
//To use this version (which provides some extra option, you will first need to download
//the PHPmailer class from: http://phpmailer.sourceforge.net/
//Then install it on your server in the same directory that you desire to put this script in.
//You can get free and friendly help with this script at the
//Christian Web Masters forums: www.christian-web-masters.com/forums

//Copy and paste this code into your PHP page, where you want
//the Tell a Friend Form to appear.
//Note if you do not know PHP, just make a regular HTML page
//and save it as a .php file.

//You can also save this as a .php file such as "tell.php"
//and include it in the php file(s) of your choice
//using: include("tell.php");

//Then just link to this page from wherever you want.

//You will need to configure the script.
//It's easy, just use a text editor.
//Follow the instructions in the "Begin Configure" section below.

//Note: This script contains a link to our site.
//We'd like it if you left it, but you have permission to remove it. :)

//This script uses the new $_POST and $_SERVER['HTTP_REFERRER']
//If you have an issue with an older version of PHP, you can get help at:

// ************Begin Configure***************

//Put your website name between the quotes below:
$websiteName = "Songs from the Word";

//Put your website address between the quotes below:
$websiteAddress = "www.SongsfromtheWord.com";

// Change the 0 to a 1 below,
//if you want to recieve a notice when people are refered:
$notice = 1;


//Put your email address in the " " if you changed the notice to 1
$adminEmail = "songsfromtheword@earthlink.net";


//Put the subject line text you want the email to read in the "":
$subject = "Thinking of you";

// Put your default message intro text in the first set of quotes below
//This is what the people who are referred will see
//along with any personal message entered by the referer.
$defaultMessageIntro = "Thought you would enjoy this site: "www.songsfromtheword.com "\n" ;


//Put in the "" your default close (this will be at the end of the message):
$defaultMessageClose = "Songs from the Word offers resources of encouragement in the Christian walk, including books, songs, and artwork. The website is at: http://www.songsfromtheword.com";


// ************End Configure****************

//Bring in the PHPMailer class
require("class.phpmailer.php");

// Set the link that will be in intro/invite and used to send the referer back
if (isset($_POST['link'])) {
$link = $_POST['link'];
}
else {
if (empty($_SERVER['HTTP_REFERER'])) {
$link = 'http://' . $websiteAddress;
} else {
$link = $_SERVER['HTTP_REFERER'];

}
}
// Add the link to the intro
$defaultMessageIntro = $defaultMessageIntro . $link . "\n";

//Adds a space infront of the subject line (to add a name latter
$subject = ' ' . $subject;
?>
<?php
function doTellForm ($privacyPolicyLinkText, $privacyPolicyLinkURL, $defaultMessageIntro, $link)
{
// If you understand HTML, you can make changes to the form layout below.
// I just copy the HTML from the "<form" opening to the "</form>"
// Put it into Dreamweaver (WYSIWYG Editor), work on it and put it back.
$theForm = <<<EOD
<form name="tellForm" method="post" action="">
<table width="500px" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="50%"><div align="center">Your name:</div></td>
<td width="50%"><div align="center">Your email:</div></td>
</tr>
<tr>
<td> <div align="center">
<input name="your_name" type="text" id="your_name">
</div></td>
<td> <div align="center">
<input name="your_email" type="text" id="your_email">
</div></td>
</tr>
<tr>
<td><div align="center"></div></td>
<td><div align="center"></div></td>
</tr>
<tr>
<td><div align="center">Enter the name(s) of your friend(s):</div></td>
<td><div align="center">Enter the email(s) of your friend(s):</div></td>
</tr>
<tr>
<td> <div align="center">
<input name="friend_name1" type="text" id="friend_name1">
</div></td>
<td width="50%"> <div align="center">
<input name="friend_email1" type="text" id="friend_email1">
</div></td>
</tr>
<tr>
<td> <div align="center">
<input name="friend_name2" type="text" id="friend_name2">
</div></td>
<td> <div align="center">
<input name="friend_email2" type="text" id="friend_email2">
</div></td>
</tr>
<tr>
<td> <div align="center">
<input name="friend_name3" type="text" id="friend_name3">
</div></td>
<td> <div align="center">
<input name="friend_email3" type="text" id="friend_email3">
</div></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<p>Your Message (optional):
<input name="tellsubmit" type="hidden" id="tellsubmit4" value="doTheSend">
<input name="link" type="hidden" id="link" value="$link">
<br>
<textarea name="message" cols="50" rows="5" id="textarea">$defaultMessageIntro</textarea>
</p>
</div></td>
</tr>
<tr>
<td colspan="2"><p align="center">
<input type="submit" name="Submit" value="Send It">
</p>
<p align="center"><a href="$privacyPolicyLinkURL" target="_blank">$privacyPolicyLinkText</a><br>
<a href="http://www.christian-web-masters.com" target="_blank">free php scripts</a></p></td>
</tr>
</table>
<div align="center">
</div>
</form>
EOD;
echo ($theForm);
}
?>

<?php
function spamcheck($array) {
# returns true if data is ok, otherwise false if it is spam-looking
return (!preg_match("/(MIME-Version:|Content-Type:|\n|\r)/i", join('',array_values($array)) ));
}

function doTell ($notice, $adminEmail, $subject, $websiteName, $defaultMessageClose, $link)
{
//Create a new mail object. You'll get an error here is the right files are not required
//at the top of this script.
$mail = new phpmailer();
// set mailer to use SMTP
$mail->IsSMTP();
//Specify the use of the local server.
//Should not have to authenticate.
//If you get an error sending, use the Christian-Web-Masters.com forums to ask
//How to change this script to use another server.
//Or read the documentation for PHPMailer.
$mail->Host = "localhost";

//If the site visitor entered thier email on the form,
//then the mail sent will come from them.
if ($_POST['your_email'] != "")
{
$mail->From = $_POST['your_email'];
$mail->FromName = $_POST['your_name'];
}
//If not, the email will come from your admin email address.
else
{
$mail->From = $adminEmail;
$mail->FromName = $websiteName;
}

if ($_POST['friend_email1'] != "")
{
// Check for suspected spam content
if (!spamcheck(array($_POST['friend_email1'],$subject,$_POST['friend_name1']))) {
die('no spam please');
}

//This takes the email address and name from form variables
$mail->AddAddress($_POST['friend_email1'], $_POST['friend_name1']);
//This tacs the name onto the subject line
$mail->Subject = $_POST['friend_name1'] . $subject;
//This tacs the name onto the message
$mail->Body = $_POST['friend_name1'] . "\r\n" . $_POST['message']
. "\n\n" . $defaultMessageClose;

//If you kept the notice set to 1, then this will Blind Copy you on the email.
if ($notice == 1)
{
$mail->AddBCC ($adminEmail, 'Referal notice');
}

//If the mail does not send...
if(!$mail->Send())
{
// This echo's the error message if the email did not send.
// You could change the text in between the <p> tags.
echo('<p>Mail could not be sent to '
. $_POST['friend_email1'] . ' Please use your back button to try them again.</p>');
}
//If the mail does send...
else
{
echo ('<p>Your message was successfully sent to ' . $_POST['friend_email1'] . '</p>');
}
// Clear all addresses and attachments for the next one
$mail->ClearAddresses();
}
if ($_POST['friend_email2'] != "")
{
// Check for suspected spam content
if (!spamcheck(array($_POST['friend_email3'],$subject,$_POST['friend_name3']))) {
die('no spam please');
}

//This takes the email address and name from form variables
$mail->AddAddress($_POST['friend_email2'], $_POST['friend_name2']);
//This tacs the name onto the subject line
$mail->Subject = $_POST['friend_name2'] . $subject;
//This tacs the name onto the message
$mail->Body = $_POST['friend_name2'] . "\r\n" . $_POST['message']
. "\n\n" . $defaultMessageClose;

//If you kept the notice set to 1, then this will Blind Copy you on the email.
if ($notice == 1)
{
$mail->AddBCC ($adminEmail, 'Referal notice');
}

//If the mail does not send...
if(!$mail->Send())
{
// This echo's the error message if the email did not send.
// You could change the text in between the <p> tags.
echo('<p>Mail could not be sent to '
. $_POST['friend_email2'] . ' Please use your back button to try them again.</p>');
}
//If the mail does send...
else
{
echo ('<p>Your message was successfully sent to ' . $_POST['friend_email2'] . '</p>');
}
// Clear all addresses and attachments for the next one
$mail->ClearAddresses();
}
if ($_POST['friend_email3'] != "")
{
// Check for suspected spam content
if (!spamcheck(array($_POST['friend_email3'],$subject,$_POST['friend_name3']))) {
die('no spam please');
}

//This takes the email address and name from form variables
$mail->AddAddress($_POST['friend_email3'], $_POST['friend_name3']);
//This tacs the name onto the subject line
$mail->Subject = $_POST['friend_name3'] . $subject;
//This tacs the name onto the message
$mail->Body = $_POST['friend_name3'] . "\r\n" . $_POST['message']
. "\n\n" . $defaultMessageClose;

//If you kept the notice set to 1, then this will Blind Copy you on the email.
if ($notice == 1)
{
$mail->AddBCC ($adminEmail, 'Referal notice');
}

//If the mail does not send...
if(!$mail->Send())
{
// This echo's the error message if the email did not send.
// You could change the text in between the <p> tags.
echo('<p>Mail could not be sent to '
. $_POST['friend_email3'] . ' Please use your back button to try them again.</p>');
}
//If the mail does send...
else
{
echo ('<p>Your message was successfully sent to ' . $_POST['friend_email3'] . '</p>');
}
// Clear all addresses and attachments for the next one
$mail->ClearAddresses();
}

$return = <<<EOD
<p align="center"><a href="$link">Return to the page you left.</a></p>
EOD;

echo ($return);
}


if (isset($_POST['tellsubmit'])) {
doTell($notice, $adminEmail, $subject, $websiteName, $defaultMessageClose, $link);
}
else {
doTellForm($privacyPolicyLinkText, $privacyPolicyLinkURL, $defaultMessageIntro, $link);
}
?>