When does send() fail???

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

When does send() fail???

Post by alex.barylski »

I notice most of the examples use a conditional to check the result of send() but from experimentation I also see that an exception is thrown whenever the connection is faulty (wrong credentials, host, etc). In this case I prefer just calling the send() and handling errors using a try-catch block - which is what I've done.

Does the conditional serve any other purpose or was strictly for backwards compatability with PHP 4 versions? Can the return value tell me anything the exceptions cannot?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: When does send() fail???

Post by Chris Corbyn »

The conditional is different. Exceptions are thrown if the SMTP server gives an error response or if the connection fails. Not sending to one address isn't really worthy of throwing an exception since all the other addresses you needed to send to would then subsequently be halted. send() returns false (actually it returns 'int 0' for "zero successful addresses", usually it returns a positive integer on success). If the SMTP server rejects the address you're sending to, but that's fine, SMTP allows you to just "ignore" the error and carry on as usual.

EDIT | Just to clarify, it's when you say who you're sending "to" that dictates the value returned from send(). If you say you're sending "from" no-such-user@no-such-domain.tld then the server will likely say "bugger off" and you can't physically get as far as telling it who you're sending to in that case, hence an Exception.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: When does send() fail???

Post by Chalks »

Chris Corbyn wrote:then the server will likely say "bugger off"
The Server wrote:Parse error: syntax error, unexpected BUGGER OFF in /error.php on line 1
Post Reply