I have a monthly newsletter that currently goes out to 1000+ recipients. The only customization for each recipient is the subscription management link (need to dynamically add their email address to the url).
This is what I am using and it works great, but I am concerned that two calls to the database which are essentially getting the same info is less efficient than it should be. I also have another user list which is 30,000 recipients which I am concerned about, although this is about a 'once a year' list.
Code: Select all
set_time_limit(0);
ignore_user_abort();
flush();
ob_flush();
require_once ($_SERVER['DOCUMENT_ROOT'].'/swift/Swift.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/swift/Swift/Connection/SMTP.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/swift/Swift/Plugin/Decorator.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/swift/Swift/Plugin/AntiFlood.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/swift/Swift/Plugin/VerboseSending.php');
//Enable disk caching if we can
if (is_writable("/tmp"))
{
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath("/tmp");
}
$swift =& new Swift(new Swift_Connection_SMTP("my.smtp.host"));
//Reconnect after 200 emails, but wait for 10 seconds first
$swift->attachPlugin(new Swift_Plugin_AntiFlood(200, 10), "anti-flood");
$view =& new Swift_Plugin_VerboseSending_DefaultView();
$swift->attachPlugin(new Swift_Plugin_VerboseSending($view), "verbose");
//Create a message
$message =& new Swift_Message(stripslashes ($_POST['email_subject']));
//Add some "parts"
$message->attach(new Swift_Message_Part(stripslashes($_POST['text_only'])));
$message->attach(new Swift_Message_Part(stripslashes ($_POST['html_code']), "text/html"));
//Initiate recipient list
$recipients =& new Swift_RecipientList();
$email_address = $_POST['email_address'];
//Extend the replacements class
class Replacements extends Swift_Plugin_Decorator_Replacements {
function getReplacementsFor($address) {
$query = "SELECT email_address as '{EMAIL_ADDRESS}' FROM general WHERE email_address = '" . mysql_real_escape_string($address) . "'";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
{
return mysql_fetch_assoc($result);
}
}
}
$query_emails = "SELECT email_address FROM general";
$emails = mysql_query($query_emails) or die(mysql_error());
while($row_emails = mysql_fetch_assoc($emails)){
$recipients->addTo($row_emails['email_address']);
}
//Load the plugin with the extended replacements class
$swift->attachPlugin(new Swift_Plugin_Decorator(new Replacements()), "decorator");
//Send messages
$sent = $swift->batchSend($message, $recipients, new Swift_Address("me@mydomain.com","Me"));
//Disconnect from SMTP, we're done
$swift->disconnect();
if ($sent)
{
print "Success";
}
else
{
print "Failure";
}I am wondering if perhaps making use of setBody in my while loop might actually be more efficient. ie something based on the code snippets below that I grabbed from some other posts here on the forum.
Code: Select all
$html_part->setBody(str_replace("{EMAIL_ADDRESS}", $email_address, $html_body));
$text_part->setBody(str_replace("{EMAIL_ADDRESS}", $email_address, $text_body));
$message->setBody(str_replace(array_keys($replacements), array_values($replacements), $body));Adrian
PS Have a great time in Australia - even though I live in Canada now, it is my homeland