Error: [421 too many messages in this connection]

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
bruno_mac
Forum Newbie
Posts: 6
Joined: Mon Jan 14, 2008 3:34 pm

Error: [421 too many messages in this connection]

Post by bruno_mac »

Hi!

I got this error when sending the emails to a list of clients from a database. Am I supposed to close and re-open the Swift connection after XX number of emails are sent out?
This is how I am sending the messages:

Code: Select all

 
while ($row = mysql_fetch_array ($result)){
    $recipient_id = $row['recipient_id'];
    $list_id = $row['list_id'];
 
    $replacements = array($row["recipient_email"] => array("{UNSUBSCRIBE_EMAIL}" => $row["recipient_email"], 
                                        "{LIST_ID}" => $row["list_id"], 
                                        "{FIRST_NAME}" => $row["recipient_first_name"], 
                                        "{LAST_NAME}" => $row["recipient_last_name"]));
    
    //Load the plugin with these replacements
    $swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
 
    //Replace content in the $parts, this will still affect $message due to the references
    //$htmlPart->setBody(str_replace("{UNSUBSCRIBE_EMAIL}", $row["recipient_email"], $html_body));
    //$textPart->setBody(str_replace("{UNSUBSCRIBE_EMAIL}", $row["recipient_email"], $text_body));
 
    //set the number of messages per minute
    $throttler->setEmailsPerMinute(100); 
    $swift->attachPlugin($throttler, "throttler");
 
    if ($swift->send($message, $row['recipient_email'], new Swift_Address($message_from_email, $message_from_name))){
            //echo "Sent";
            }
                else {echo "Failed";}
 
What is the problem with my sending? By the way, I got the error message exactly after sending 1000 emails out.

Thanks a lot for the help.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Error: [421 too many messages in this connection]

Post by Chris Corbyn »

Take a look at the AntiFlood plugin....
bruno_mac
Forum Newbie
Posts: 6
Joined: Mon Jan 14, 2008 3:34 pm

Re: Error: [421 too many messages in this connection]

Post by bruno_mac »

Thanks. I completely forgot about that great plugin.

Should I set those plugins (anti-flood and throttler) inside my "while" loop of email addresses?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Error: [421 too many messages in this connection]

Post by Chris Corbyn »

bruno_mac wrote:Thanks. I completely forgot about that great plugin.

Should I set those plugins (anti-flood and throttler) inside my "while" loop of email addresses?
No, just set them before sending. Generally speaking when looping you want to do as little possible inside the loop otherwise you're running the same code thousands of times over ;)
bruno_mac
Forum Newbie
Posts: 6
Joined: Mon Jan 14, 2008 3:34 pm

Re: Error: [421 too many messages in this connection]

Post by bruno_mac »

Thanks.
But on my code snippet above, I have all those plugins and settings inside my loop. This loops only goes through all the email addresses. So I should bring all those out of the loop?

Inside my loop then, I will only have the Replacement plugin, which takes a different value for each message and the actual sending. Is this correct?

Thanks a lot.
Bruno.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Error: [421 too many messages in this connection]

Post by Chris Corbyn »

You should really be using batchSend() and putting the replacements plugin only once. What you're currently doing is a little wasteful. Take a look at the notes on "Sending to multiple recipients" and then again at the notes on the Decorator plugin in the wiki ;)
Post Reply