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
dillion
Forum Commoner
Posts: 56 Joined: Thu Feb 15, 2007 8:32 am
Post
by dillion » Thu May 22, 2008 3:24 am
For the life of me, I can't remember how to continue the Swiftmail process rather than breaking out if the email address is invalid.
I had a cron set up to send out emails at 1am, and this morning I received a cron email displaying error:
PHP Fatal error: Uncaught exception 'Exception' with message 'The recipients parameter must either be a valid string email address, an instance of Swift_RecipientList or an instance of Swift_Address.' in <PATH TO CLASS.SWIFTMAIL.PHP>:337
Stack trace:
#0 <PATH TO PHP> (58): Swift->send(Object(Swift_Message), 'me@ohoh...', 'email@domain...')
#1 {main}
thrown in <PATH TO CLASS.SWIFTMAIL.PHP> on line 337
Turns out that the email address was invalid (me@ohoh>com rather than
me@ohoh.com ).
I thought
Code: Select all
// call swiftmail class
......
$sent = $swift->send($message, $row['subscriberemailaddress'], "from@domain.com"); // Try sending the email
if(!$sent) {
// error sending
$param_sent = "0";
$param_status = ":: SENDFAILED";
$log = Swift_LogContainer::getLog();
$param_statusmessage = $log->dump(true);
$log->addFailedRecipient($row['subscriberemailaddress']);
}
else {
// send was successful
$param_sent = "1";
$param_status = ":: SENDSUCCESS";
$log = Swift_LogContainer::getLog();
$param_statusmessage = $log->dump(true);
}
// do mySQL stuff
.....
Am I missing something silly somewhere?
dillion
Forum Commoner
Posts: 56 Joined: Thu Feb 15, 2007 8:32 am
Post
by dillion » Wed May 28, 2008 3:29 am
bump.... Any ideas?
What I don't get is why if $sent failed then surely it should log (e.g. $param_sent = "0") then carry on to next email address rather than fail completely?
Code: Select all
$sent = $swift->send($message, $row['subscriberemailaddress'], "from@domain.com"); // Try sending the email
if(!$sent) {
// error sending
$param_sent = "0";
$param_status = ":: SENDFAILED";
$log = Swift_LogContainer::getLog();
$param_statusmessage = $log->dump(true);
$log->addFailedRecipient($row['subscriberemailaddress']);
}
else {
// email was sent
}
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Wed May 28, 2008 9:12 pm
The exception is thrown before the logger gets a chance to log it. The reason is that sending is never even attempted because the address is not valid. I could probably have changed that but v4 won't behave the same anyway.
Code: Select all
try
{
$sent = $swift->send( ... );
}
catch (Exception $e)
{
//Handle failure here
}
dillion
Forum Commoner
Posts: 56 Joined: Thu Feb 15, 2007 8:32 am
Post
by dillion » Thu May 29, 2008 6:25 am
thanks mate for your response. I already have a try catch statement:
Code: Select all
try {
//Start Swift
$swift =& new Swift(new Swift_Connection_SMTP($mail_host));
$log =& Swift_LogContainer::getLog();
$log->setLogLevel(4);
// load a Anti flood plugin
$swift->attachPlugin(new Swift_Plugin_AntiFlood($smtp_flood_run, $smtp_flood_pause), "anti-flood");
do {
$message =& new Swift_Message("subject");
$message->attach(new Swift_Message_Part("message"), "text/plain"));
$message->attach(new Swift_Message_Part("<b>message</b>), "text/html"));
$message->setReturnPath("mail@domain.com");
$sent = $swift->send($message, $row['emailaddress'], "noreply@domain.com"); // Try sending the email
if(!$sent) {
// error sending
$param_sent = "0";
$param_status = ":: SENDFAILED";
$log = Swift_LogContainer::getLog();
$param_statusmessage = $log->dump(true);
$log->addFailedRecipient($row['emailaddress']);
}
else {
// send was successful
$param_sent = "1";
$param_status = ":: SENDSUCCESS";
}
// log status
} while ($row = mysql_fetch_assoc($set));
$swift->disconnect();
} catch (Swift_ConnectionException $e) {
echo "There was a problem communicating with SMTP :" . $e->getMessage();
$swift->reset();
exit();
} catch (Swift_Message_MimeException $e) {
echo "There was an unexpected problem building the email: " . $e->getMessage();
$swift->reset();
exit();
}
I take it that I need to add another try catch statement or place it inside the loop?
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Fri May 30, 2008 9:28 pm
I have to wonder what I was thinking by throwing an Exception which isn't subclassed. The problem now is that if you place a try/catch inside the other you'd have to catch everything of type "Exception" which would break things. I can only provide you with a dirty hack I'm afraid.
Code: Select all
try {
//Start Swift
$swift =& new Swift(new Swift_Connection_SMTP($mail_host));
$log =& Swift_LogContainer::getLog();
$log->setLogLevel(4);
// load a Anti flood plugin
$swift->attachPlugin(new Swift_Plugin_AntiFlood($smtp_flood_run, $smtp_flood_pause), "anti-flood");
do {
try {
$message =& new Swift_Message("subject");
$message->attach(new Swift_Message_Part("message"), "text/plain"));
$message->attach(new Swift_Message_Part("<b>message</b>", "text/html"));
$message->setReturnPath("mail@domain.com");
$sent = $swift->send($message, $row['emailaddress'], "noreply@domain.com"); // Try sending the email
if(!$sent) {
// error sending
$param_sent = "0";
$param_status = ":: SENDFAILED";
$log = Swift_LogContainer::getLog();
$param_statusmessage = $log->dump(true);
$log->addFailedRecipient($row['emailaddress']);
}
else {
// send was successful
$param_sent = "1";
$param_status = ":: SENDSUCCESS";
}
// log status
} catch (Exception $e) {
if (get_class($e) != 'Exception') { //Re-throw it if not just "Exception"
throw $e;
}
}
} while ($row = mysql_fetch_assoc($set));
$swift->disconnect();
} catch (Swift_ConnectionException $e) {
echo "There was a problem communicating with SMTP :" . $e->getMessage();
$swift->reset();
exit();
} catch (Swift_Message_MimeException $e) {
echo "There was an unexpected problem building the email: " . $e->getMessage();
$swift->reset();
exit();
}
dillion
Forum Commoner
Posts: 56 Joined: Thu Feb 15, 2007 8:32 am
Post
by dillion » Tue Jun 03, 2008 3:39 am
very much appreciated mate!
I take it that version 4 won't have this issue?
Btw, hope all is still going well in Australia.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Tue Jun 03, 2008 7:15 am
dillion wrote: very much appreciated mate!
I take it that version 4 won't have this issue?
Btw, hope all is still going well in Australia.
No it won't. It's not so aggressive at throwing exceptions and it certainly won't throw anything which is only "Exception"!