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?
When does send() fail???
Moderators: Chris Corbyn, General Moderators
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: When does send() fail???
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.
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.
Re: When does send() fail???
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