Send() and exceptions?

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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Send() and exceptions?

Post by alex.barylski »

I'm curious, but when you send an email and the send() function fails, currently assume this to mean a soft bounce log it and continue on my way.

I just got to thinking though, what happens if the SMTP pukes and I have a hundre or so more emails to iterate - I assume send() just returns false/zero?

Does send() perform any kind of connection test and throw an exception when the connection dies midway through a send?

I use a single connection to send all emails, so I need to figure out a way to detect whether the connection choked and no more emails will send or if false is returned or zero then I will just assume a soft bounce.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Send() and exceptions?

Post by Chris Corbyn »

Yes, an exception will be thrown if the connection throws a wobbly.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Send() and exceptions?

Post by alex.barylski »

Ok cool...

I could have looked at the code to confirm my suspicions...sorry about that.

May I make one small suggestion? Swift::send() as a last argument accepts the sender address, either as a string or Swift_Address object.

I think it would be nice if the argument was optional (assigned NULL by default) to indicate that From: has been set in the $message object and those details should be used??? Make sense or am I missing something?

In my case, I already set the From: details before calling send() so passing that object in again on each call is redundant.

Upon further inspection, I see this line:

Code: Select all

if (!($has_from = $message->getFrom())) $message->setFrom($from);
It looks as though the code already performs this check and uses the preset From: details so I should be able to just pass null to the argument and not have to touch your code at all???

Cheers :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Send() and exceptions?

Post by Chris Corbyn »

New version reads addresses from the message object and not from a separate list. In v4 it's a simple:

$mailer->send($message);
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Send() and exceptions?

Post by alex.barylski »

Edit: I'm guessing you would set the To: via the message as well...so somethinglike:

Code: Select all

$message->setTo(new Swift_Address('Name', 'email@domain.com'));
$swift->send($message);
--- Old message ---

If you call send() from inside a loop which iterates email addresses, how do you specify To: addresses?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Send() and exceptions?

Post by Chris Corbyn »

I've dropped the Swift_Address class in v4... I figured an associative array works just a well and is less memory intensive. Having all those Swift_Address objects gets a bit silly.

Code: Select all

$message->setTo(array('email@domain.com'=>'Name'));
$swift->send($message);
Of course, for multiple To: addresses you just add more to the array, and for addresses with no name, you just leave out the key (or use NULL as the name).

send() will treat it as if all the recipients are to be displayed in the Headers of the message, but batchSend() will behave as it does now, and send to each person as a separate email (with their own headers).

Most of this stuff is nearly finished in v4. It's all the Transport classes and a centralized event dispacthing system I need to focus on now.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Send() and exceptions?

Post by alex.barylski »

Chris Corbyn wrote:I've dropped the Swift_Address class in v4... I figured an associative array works just a well and is less memory intensive. Having all those Swift_Address objects gets a bit silly.

Code: Select all

$message->setTo(array('email@domain.com'=>'Name'));
$swift->send($message);
Of course, for multiple To: addresses you just add more to the array, and for addresses with no name, you just leave out the key (or use NULL as the name).

send() will treat it as if all the recipients are to be displayed in the Headers of the message, but batchSend() will behave as it does now, and send to each person as a separate email (with their own headers).

Most of this stuff is nearly finished in v4. It's all the Transport classes and a centralized event dispacthing system I need to focus on now.

Sexy...very sexy...only one small concern. My code would require some significant refactoring, unless I can just change a message To: address on the fly during each send() invocation. Basically passing the array would cause issues.

So I needed setTo() to override the previous details, would it? Or does each item you add get appended to the array? Being able to use either technique would be best and I assume this is possible as setters will typically just override the existing member - but just incase you choose to append instead of over-write, may I ask??? :)
Post Reply