Page 1 of 1

Fatal error: Uncaught exception 'Swift_ConnectionException'

Posted: Wed Nov 07, 2007 8:06 pm
by wheeler
Hello,

I use this php script to send out emails:

Code: Select all

function email($to, $subject, $body, $format='html', $replyto=E_FROM, $priority=3)
{
	require_once "swift/Swift.php";
	require_once "swift/Swift/Connection/SMTP.php";
	
	$swift = new Swift(new Swift_Connection_SMTP("mail1.myserver.com"));
	
	//Create the message
	if ($format == 'html')
	{
		$email = new Swift_Message($subject, $body, "text/html");
	} elseif ($format == 'plain') {
		$email = new Swift_Message($subject, $body);
	}
	
	// set priority if it is not default 3 -- 1=high, 3=normal, 5=low
	if ($priority != 3) $email->setPriority($priority);
	
	$email->setReturnPath(E_BOUNCE);
	$email->setReplyTo($replyto);
	
	// from name is always E_FROM_NAME ($site_name)
	$sender = new Swift_Address(E_FROM, E_FROM_NAME);
		 
	//Now check if Swift actually sends it
	if ($swift->send($email, $to, $sender)) 
	{
		return true;
	} else {
		return false;
		error();
	}
	
	$email->disconnect();
}
I use this script to send all emails. Usually every 10 minutes, up to 1000 emails would be sent in a loop using this function. But the average sendout is probably between 10-30 and it works fine. Even when I send a newsletter (where 1000 every 10 minutes is sent) I have absolutely no problems - besides a full mail queue due to alot of failing addresses.

The other info that may be helpful is we're running a dedicated LAMP server, it is running well under capacity.

Sporadically, I get this error:

Code: Select all

Fatal error: Uncaught exception 'Swift_ConnectionException' with message 'There was a problem reading line 1 of an SMTP response. The response so far was:<br />[].  It appears the connection has died without saying goodbye to us! Too many emails in one go perhaps? (fsockopen: #0) ' in /srv/www/www.mysite.com/public_html/includes/swift/Swift/Connection/SMTP.php:250
Stack trace:
#0 /srv/www/www.mysite.com/public_html/includes/swift/Swift.php(306): Swift_Connection_SMTP->read()
#1 /srv/www/www.mysite.com/public_html/includes/swift/Swift.php(427): Swift->command('??.', 250)
#2 /srv/www/www.mysite.com/public_html/includes/functions.php(1318): Swift->send(Object(Swift_Message), 'tandina@bigpond...', Object(Swift_Address))
#3 /srv/www/www.mysite.com/public_html/emails/item-won.php(14): email('tandina@bigpond...', 'You won FACETED...', '<html><body>?<h...')
#4 /srv/www/www.mysite.com/scripts/subscripts/closed-auctions.php(50): include('/srv/www/www.my...')
#5 /srv/www/www.mysite.com/scrip in /srv/www/www.mysite.com/public_html/includes/swift/Swift/Connection/SMTP.php on line 250
I got 6 of these this morning, i've seen it a handful of times in the last 6 months.

Posted: Sun Nov 11, 2007 4:48 pm
by Chris Corbyn
This happens when the network is having problems (serious packet loss?) or the server is under high load (it doesn't respond in time) ;)

Posted: Sun Nov 11, 2007 7:44 pm
by wheeler
i'm going to hazard a guess and say the server load is to blame - after all I might be asking for say 50 new connections in about 1 second. I've added a usleep of 1/5 of a second between sends, will see if that helps.

Would the throttler work?

Posted: Tue Dec 11, 2007 2:18 pm
by iricketson
Just so happens that I ran into this issue today. Would the Throttler plugin be a viable solution to this issue? I see that it does sleep() for a specified amount of time, so I would assume this would help lower the load.

What's more, could the AntiFlood and the Throttler plugin work together hand in hand? Are there any complications by using both of these at the same time?

Any comments on this approach?

Re: Would the throttler work?

Posted: Tue Dec 11, 2007 9:11 pm
by Chris Corbyn
iricketson wrote:Just so happens that I ran into this issue today. Would the Throttler plugin be a viable solution to this issue? I see that it does sleep() for a specified amount of time, so I would assume this would help lower the load.

What's more, could the AntiFlood and the Throttler plugin work together hand in hand? Are there any complications by using both of these at the same time?

Any comments on this approach?
Throttler would certainly help. Throttler can be used with AntiFlood. Never send more than 100 emails per connection (i.e. if you have 1000 emails to send, use AntiFlood to disconnect after each 100).

The best solution however is to run cron more often with lower quantities of emails.

Posted: Wed Dec 12, 2007 6:25 pm
by kdecapite
I just ran into the same error myself. I'm using the AntiFlood and Throttler plugins to send to a list of 7500 emails (using Swift_BatchMailer to handle the send operation). I have Throttler keeping the email sending to 30 per minute (1 email every 2 seconds) and AntiFlood pauses for 10 seconds after every 100 emails. I'm using only one SMTP server.

Last week I sent separate batches in the following quantities:
100, 200, 500, 750, 1500, 3000, 1500

Those all worked great. Then just today I tried to increase the quantity to 5000 and I received this uncaught exception error. Obviously, I am primarily interested in keeping the exception from being thrown in the first place but in the meantime, how can I trap this exception so the script will continue to send after the exception is thrown? Or is this not possible?

Also, how can I tell where in the list of emails the script terminated? I don't want to resend to the same recipient when I resume the send operation.