Mass mailing procedure

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
dillion
Forum Commoner
Posts: 56
Joined: Thu Feb 15, 2007 8:32 am

Mass mailing procedure

Post by dillion »

I currently use this way:

Code: Select all

 
// loop
do {
    
    // log status of delivery, e.g. save data in database
    $insert_sql = sprintf("INSERT INTO xxxxxxxxxx");
 
    $result = mysql_query($insert_sql, $data);
 
    $replacements = array(
        $para_useremail => array("{WEBSITE_URL}" => $website_url, "{MESSAGE}" => $para_message)
    );
 
    
    // send email to website contact
    $swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
    $sent = $swift->send($message, $para_useremail, $mail_emailsource); // Try sending the email
 
    if(!$sent) {
        // error sending
        $para_status = ":: SENDFAILED";
        $log =& Swift_LogContainer::getLog();
        $para_description = $log->dump(true);
 
        $update_sql = sprintf("UPDATE xxxxx");
 
        $result = mysql_query($update_sql, $data);
    }
    else {
        // send was successful
        
        $update_sql = sprintf("UPDATE xxxxx");
 
        $result = mysql_query($update_sql, $data);
    
 
    }
                
} while ($row_subscriber_set = mysql_fetch_assoc($subscriber_set));
 
Is this a "good" method? Would it be better to use something like a batchsend instead? e.g.:

Code: Select all

 
$recipients =& new Swift_RecipientList();
do {
   $recipients->addTo($para_useremail, $para_username);
} while ($row_subscriber_set = mysql_fetch_assoc($subscriber_set));
 
$swift=& new Swift_BatchMailer($swift);
$swift->send($message, $recipients, new Swift_Address($mail_emailsource, "Our Name");
// once sent is done, use $swift->getFailedRecipients(); result to update database
 
If my current set-up is bad and it is better to use batchsend, how would you use Decorator or AntiFlood plugins?

Many thanks in advance! :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Mass mailing procedure

Post by Chris Corbyn »

You shouldn't be loading any plugins more than once, so putting the calls to attachPlugin() in the loop is wrong. By doing that you're constantly overriding the plugin settings and thus, something like AntiFlood will never work.

send() also throws exceptions which will potentially halt your script so you'd ideally want to wrap that call in a try/catch.

Using batchSend() will solve both of those problems... just attach the decorator before calling batchSend(), and the same for the AntiFlood plugin. All you need to do is build your entire array of replacements before loading the plugin.
dillion
Forum Commoner
Posts: 56
Joined: Thu Feb 15, 2007 8:32 am

Re: Mass mailing procedure

Post by dillion »

cheers for your response. I take it that using batchsend is better and I should modify my script accordingly?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Mass mailing procedure

Post by Chris Corbyn »

dillion wrote:cheers for your response. I take it that using batchsend is better and I should modify my script accordingly?
It's not better as such... batchSend() just calls send() itself. It's just easier for end-users who don't really care about what goes wrong and would rather let swift handle it ;) If you'd be happy wrapping your send() calls in a try/catch then do that ;) One thing to note is that inside the catch block you'll more than likely want to call $swift->reset() for safe measure.

Meh, just use batchSend() :)
dillion
Forum Commoner
Posts: 56
Joined: Thu Feb 15, 2007 8:32 am

Re: Mass mailing procedure

Post by dillion »

Chris Corbyn wrote: It's not better as such... batchSend() just calls send() itself. It's just easier for end-users who don't really care about what goes wrong and would rather let swift handle it ;) If you'd be happy wrapping your send() calls in a try/catch then do that ;) One thing to note is that inside the catch block you'll more than likely want to call $swift->reset() for safe measure.

Meh, just use batchSend() :)
Heheh - make up your mind! :wink:

My current code already has a try/catch but no reset() call. Either way, I will modify the code and use batchSend(). :)

thanks again for your advice.
Post Reply