<?
session_start();
set_magic_quotes_runtime(0);
$sep = PATH_SEPARATOR;
ini_set("include_path", ini_get("include_path") . $sep . "./phpmailer");
$action = (isset($_POST['action']) ? $_POST['action'] :
(isset ($_GET['action']) ? $_GET['action'] :
(isset($_SESSION['action']) ? $_SESSION['action'] : "view")));
$_SESSION['action'] = $action;
#************************************************************************
//PARAMETERS
$filename = "addresses.csv" ; //name of address file
$myemailaddress = "email@domain.com" ; //email address FROM which mails are to be sent
$myname= "John Smith"; //you real name
$emailfield = 2 ; //the column in the csv file in which email addresses are stored (NB col1 = 0)
$htmlmessage = "filename.html"; //filename of the html you want to use for the email.
$textmessage = "textmessage.txt"; //filename of the text you want for non html readers
$subject = "New Address list attached"; //email subject line
$emailfield = 2 ; //the column in the csv file in which email addresses are stored (NB col1 = 0)
$namefield= 0; //the column in which the person's name is stored in the csv file (nb col1=0)
#************************************************************************
function emailcsvfiletousers() {
require("class.phpmailer.php");
global $emailfield, $filename, $myemailaddress, $myname, $htmlmessage, $textmessage, $subject, $namefield;
if (!file_exists($filename)):
return false;
endif;
$bcc = "";
//create the mail object. this assumes you have a mail server on the localhost
$mail = new PHPMailer();
$mail->Host = "localhost";
$mail->Mailer = "smtp";
$mail->From = $myemailaddress;
$mail->FromName = $myname;
$mail->AddAddress($myemailaddress,$myname);
$mail->WordWrap = 50;
$mail->AddAttachment("addresses.csv");
$mail->IsHTML(true);
$mail->Subject = $subject;
//get the mail contents
$mail->Body = @file_get_contents($htmlmessage);
$mail->AltBody = @file_get_contents($textmessage);
//this picks up the addressees from the csv fil
$fh = @fopen($filename, "rbt");
if ($fh !== false):
while (($data = fgetcsv($fh, 2048, ",")) !== FALSE):
$email = $data[$emailfield];
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)):
$mail->addBCC($email, $data[$namefield]);
endif;
endwhile;
fclose($fh);
endif;
if(!$mail->Send()):
echo "Mailer Error: " . $mail->ErrorInfo;
return false;
else:
return true;
endif;
}
//check to see whether there is a change flag
//and if there is get a list of email addresses and
//send the file to the users
if (file_exists("change.txt")):
if (emailcsvfiletousers()):
@unlink ("change.txt");
else:
@unlink ("change.txt");
//you might want to change this
endif;
endif;
$contents="";
switch ($action):
case "download":
//this allows for the direct download of the address book.
if (!file_exists("addresses.csv")):
$contents = "There are no current names in the address list";
break;
endif;
if (isset($_GET['permission']) && isset($_SESSION['permission'])):
if ($_GET['permission'] === $_SESSION['permission']):
unset ($_SESSION['permission']);
header("Content-Length: " . filesize("addresses.csv") );
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename="addresses.csv"');
header("Content-Transfer-Encoding: binary\n");
readfile("addresses.csv");
exit;
endif;
endif; //note we do not break out of this as the exit handles the dl.
case "view":
default:
//this shows the name and addresses on the page
if (!file_exists("addresses.csv")):
$contents = "There are no current names in the address list";
$flag = true;
break;
endif;
$fh = fopen("addresses.csv", "rbt");
if ($fh == false):
$contents = "There are no current names in the address list";
$flag=true;
break;
endif;
$header = true;
$col = "#FFFCD2";
while (($data = fgetcsv($fh, 2048, ",")) !== FALSE):
$col = ($col === "#FFFCD2") ? "white" : "#FFFCD2";
if ($header) $col = "white";
$contents .= "\r\n<tr style=\"background-color:$col; text-align:left;\">";
foreach ($data as $val):
if ($header===true):
$contents .= "<th>$val</th>";
else:
$contents .= "<td>$val</td>";
endif;
endforeach;
$header = false;
$contents .= "</tr>\r\n";
endwhile;
$contents = "<table border=\"1\">\r\n $contents \r\n </table>";
fclose($fh);
break;
endswitch;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Address Book</title>
</head>
<body>
<? echo $contents; if (!isset($_SESSION['permission'])) $_SESSION['permission'] = uniqid ("prm_",true);?>
<br/>
<? if (empty($flag)) : ?><h2><a href="<?=$_SERVER['PHP_SELF']?>?action=download&permission=<?=$_SESSION['permission']?>">Download</a> the latest version of the address book</h2><? endif;?>
</body>
</html>