Not sending mails...sometimes

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
munkimail
Forum Newbie
Posts: 2
Joined: Thu Nov 30, 2006 10:05 am

Not sending mails...sometimes

Post by munkimail »

Hi,

I'm banging my head against a very odd problem. The code below sends emails to an array of recipients. If $test is set to "yes", I get the mails sent to me, for testing- this works fine. If it's set to "no", the mails are supposed to go to the recipients pulled from the database, but they don't. This is very confusing, because the only difference between testing or not testing is the size and contents of the $recipients array. The full database generated $recipients array can contain between 60 and 1300 addresses.

I am relaying the mail from our local server (where Swiftmailer is running) to our web and mail server using the SMTP connection method, and it's authenticating and working fine- as demonstrated by the fact that when $test=="yes" all works fine. My host has advised me that they are using Exim, and Courier (for authentication). They have also said that they do not limit email sending, so it's not being 'choked'. I've looked at the contents of the $recipients array produced in both cases, and it is a valid array of recipients. I've looked at the contents of $swift=>transactions, and it shows that sending fails with the first RCPT command:

[6] => Array ( [command] => RCPT TO: <fisrtrecipient@email.com> [time] => 0.01258800 1164906795 [response] => 421 Unexpected failure, please try later )


Here's the code:

Code: Select all

<?php
//set some variables
$hostname = 'myserver.co.uk';
$mails_in_batch = 49;
$cooloff_time = 30;
$textonly = "Your email client is not capable of showing HTML- please upgrade in order to view this email.";
$from = "update";

//are we testing or not?
$test="no";

//Load in the components
require('Swift/Swift.php');
require('Swift/Swift/Connection/SMTP.php');
require_once('Swift/Swift/Plugin/AntiFlood.php');

$swift = new Swift(new Swift_Connection_SMTP($hostname));

//49 mails per batch with a 30 second pause between batches
$swift->loadPlugin(new Swift_Plugin_AntiFlood($mails_in_batch, $cooloff_time));


//authenticate with the server
if ($swift->authenticate($from.'@myserver.co.uk', 'mypasswordhere')){
//do nothing
}else{
	die("not authenticated");
}

//Make the script run until it's finished in the background
set_time_limit(0);
ignore_user_abort();
ob_flush();
flush();


//database connection
$conn = mysql_connect ("localhost", "myunamehere", "mypasswordhere") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("mydatabasename");

	if($test=="no"){
	$sqlstring_recipients = "SELECT * FROM mailinglist WHERE (RecipientType = 'customer') AND (IsActive = 1)";
	$sql_recipients = mysql_query($sqlstring_recipients, $conn) or die(mysql_error());
	while($row_recipients = mysql_fetch_assoc($sql_recipients)){
	$recipients[]=$row_recipients['Email'];
	}
	}else{
	$recipients=array("test@myserver.co.uk","myhotmailaddress@hotmail.com");	
	}

	$subject = "Product Update ".date("d/m/Y",time());
	$message = stripslashes($_REQUEST['body']);
	$swift->addPart($textonly);
	$swift->addPart($message,'text/html');
	$swift->addHeaders("Return-Path: ".$from."@myserver.co.uk\r\n");
	$swift->setReplyTo('"Sender Name" <'.$from.'@myserver.co.uk>');
	$swift->send($recipients, '"Sender Name" <'.$from.'@myserver.co.uk>', $subject);

$swift->close();
?>

Any help would be very very much appreciated!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Perhaps it may be useful to post the error logs.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jcart wrote:Perhaps it may be useful to post the error logs.
That little entry from the transactions log is enough :) That response has nothing to do with swift. The server is the one gettings knickers in a twist. Looks like the process may be crashing out of memory or something. Have you tried restarting the smtpd service?

If the server is saying "unexpected failure" swift can't really do much else. I'd suggest doing these in smaller batches OR use the AntiFlood plugin to give the server a 5 second rest after every 10 emails or so.
munkimail
Forum Newbie
Posts: 2
Joined: Thu Nov 30, 2006 10:05 am

Post by munkimail »

Hi- thanks for the responses. No luck I'm afraid- I've tried smaller batches (even as small as 5 mails at a time with a 10 second gap) but I get the same problem-

[21] => Array ( [command] => [time] => 0.72601000 1164971868 [response] => 220-****.******.net ESMTP Exim 4.63 #0 Fri, 01 Dec 2006 11:17:50 +0000 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail. ) [22] => Array ( [command] => EHLO *******.******.org [time] => 0.77793800 1164971868 [response] => 250-****.******.net Hello ******.******.org [**.**.**.**] 250-SIZE 52428800 250-PIPELINING 250-AUTH PLAIN LOGIN 250-STARTTLS 250 HELP ) [23] => Array ( [command] => AUTH LOGIN [time] => 0.79742900 1164971868 [response] => 334 VXNlcm5hbWU6 ) [24] => Array ( [command] => ZXhwb3J0c2FsZXNAa3Vkb3MtZGlnaXRhbC5jby51aw== [time] => 0.81453500 1164971868 [response] => 334 UGFzc3dvcmQ6 ) [25] => Array ( [command] => TmV2aWxsZQ== [time] => 0.83211900 1164971868 [response] => 235 Authentication succeeded ) [26] => Array ( [command] => MAIL FROM: [time] => 0.86728800 1164971868 [response] => 250 OK ) [27] => Array ( [command] => RCPT TO: [time] => 0.88526200 1164971868 [response] => 421 Unexpected failure, please try later ) [28] => Array ( [command] => RSET [time] => 0.91329900 1164971868 [response] => 250 Reset OK ) [29] => Array ( [command] => MAIL FROM: [time] => 0.93065200 1164971868 [response] => 421 Unexpected failure, please try later ) )

My host says there is no apparent problem with the server... :(
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Here's what that dump says in plain English :)

>> is Swift talking
<< is the SMTP server talking.....


<< Hello
>> Hello
<< Here's the extensions I have installed
......
.....
>> OK I can see I can log in, can I do that now?
<< Go for it, what's your username?
>> Blah blah
<< Great, and your password?
>> Blah blah
<< Brilliant, that looks good to me!
>> Can I send a message from xxx@yyy.com?
<< Sure
>> Can I send it to aaa@bbb.com?
<< Huh? I've fallen off my pirch
>> WTF? I don't understand, I wanted to send an email, can we start over?
<< Sure
>> Can I send an email from xxx@yyy.com?
<< Huh? I've fallen off my pirch
>> Sod it, you're not doing what I'm asking

:)

Do you want to PM me your SMTP address and I'll Telnet to it. It would be useful of you posted the dumps as they appear in View-Source too since there are bits missing (i.e. the addresses) since they are between < and > characters.
Post Reply