<?php
require_once("db_connection_details.php");
require_once 'PEAR/Mail.php';
require_once 'PEAR/mime.php';
$crlf = "\r\n";
$hdrs = array('From'=> 'my_from@my_domain.com',
'Subject' => 'Email Subject');
$hdrs['Return-Path'] = 'my_from@my_domain.com';
$target_recipients = mysql_query("select email_address from newsletter where subscription_cancelled is null and newsletter_sent = 'No' limit 25");
if ($target_recipients && mysql_num_rows($target_recipients) > 0) {
$mime = new Mail_mime($crlf);
$message_initial = file_get_contents("location/of/my/html_file.html");
while ($a_recipient = mysql_fetch_assoc($target_recipients)) {
// This line replaces their email in the body and adds in an unsubscribe code.
$message_body = str_replace("#EMAIL#",$a_recipient['email_address'],str_replace("#UNSUB_CODE#",sha1($a_recipient['email_address']."a random seed not shown here"),$message_initial));
$body_start = strpos($message_body,"<body>");
$message_inner_body = substr($message_body,$body_start,strrpos($message_body,"</body>")-$body_start);
// This is some custom formatting that I do to have both an HTML and text-only version. Change as needed, but leave something here as the setTXTBody function sets the text-only version for those who need it.
$mime->setTXTBody(strip_tags(str_replace('<div style="display:block;clear:both;width:90%;border-top:3px #0348a5 solid;"></div>','*******************************************************************',str_replace('•','*',$message_inner_body))));
//Now, add the HTML body.
$mime->setHTMLBody($message_body);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send($a_recipient['email_address'], $hdrs, $body);
//Update database to flag this email address as having the newsletter sent.
mysql_query("update newsletter set newsletter_sent='Yes' where email_address='".mysql_real_escape_string($a_recipient['email_address'])."'");
//Probably not necessary, but if errors are ever thrown then I get them in my cron email.
echo mysql_error();
}
}
?>