PHP mail() function return problem

MrMagoo

Member
Sep 6, 2005
17
0
151
I have a client who is experiencing problems with PHP mail() function returns. Currently all mail sent via sendmail is returning TRUE, regardless if the email address is invalid. Why would this be happening?

I have had my client provide me with the headers they are using below:

$return = mail("[email protected]","test","test"); print $return ? "TRUE" : "FALSE"; # TRUE is expected

$return = mail("[email protected]","test","test"); print $return ? "TRUE" : "FALSE"; # FALSE is expected
 

neo4242002

Well-Known Member
Jun 28, 2005
119
0
166
Are you reseller or dedicated user?

If you have complied php as an apache module, cpanel forward all undeliverable mails according to the email addresses in Main >> Server Contacts >> Change System Mail Preferences.

So every time you are using an invalid email address, you are sys admin will receive an email with following subject line "Mail delivery failed: returning message to sender"

Another thing to do is enable "Verify the existence of email senders." (Extra spam protection) on WHM root and change your script something like bellow

PHP:
$returnemail = should be a valid email address, perhaps the default website address.

mail($to, $subject, $message, $headers, "-f $returnemail");
Hope this help you bit but also Google for "email ejection" to learn the safety guides for your php mail functions :)
 

webignition

Well-Known Member
Jan 22, 2005
1,876
2
166
MrMagoo said:
I have a client who is experiencing problems with PHP mail() function returns. Currently all mail sent via sendmail is returning TRUE, regardless if the email address is invalid. Why would this be happening?

I have had my client provide me with the headers they are using below:

$return = mail("[email protected]","test","test"); print $return ? "TRUE" : "FALSE"; # TRUE is expected

$return = mail("[email protected]","test","test"); print $return ? "TRUE" : "FALSE"; # FALSE is expected
The purpose of the mail() function is to send mail-destined data on to sendmail (or an appropriate wrapper). If it does so successfully, it will return true, otherwise it will return false.

As you can see, this has no bearing on the validity of the data. Although uncommon, you could have an email address or user@hostname that could operate perfectly well within a local network environment.

It is therefore the responsibility of the PHP developer, and not PHP, to ensure data validity.
 

MrMagoo

Member
Sep 6, 2005
17
0
151
webignition said:
The purpose of the mail() function is to send mail-destined data on to sendmail (or an appropriate wrapper). If it does so successfully, it will return true, otherwise it will return false.

As you can see, this has no bearing on the validity of the data. Although uncommon, you could have an email address or user@hostname that could operate perfectly well within a local network environment.

It is therefore the responsibility of the PHP developer, and not PHP, to ensure data validity.
So can I assume from what you're saying that the PHP mail() function cannot return a TRUE if an email has been successfully delivered to it's intended recipient? The return functions are just internal if you like, acknowledging if it has parsed to sendmail ??? If so, is there any way to confirm a successful delivery to the outside?
 

webignition

Well-Known Member
Jan 22, 2005
1,876
2
166
MrMagoo said:
So can I assume from what you're saying that the PHP mail() function cannot return a TRUE if an email has been successfully delivered to it's intended recipient? The return functions are just internal if you like, acknowledging if it has parsed to sendmail ??? If so, is there any way to confirm a successful delivery to the outside?
Absolutely, your explanation is spot on.

I doubt that it would be possible to confirm a successful delivery to the outside and I'll explain why.

Let's imagine an email being sent through PHP. It would (very roughly) go something like this:

1. PHP passes mail to sendmail (or equivalent wrapper)
2. Mail gets passed on to and sent out by Exim
3. Mail gets sent through an indeterminable amount of MTAs
4. Mail reaches recipient mailbox

The best that could possibly be done would be for Exim to return something back to the sendmail wrapper, which in turn would pass something back to PHP, saying that the email left the server. But that by no means means that the email will actually get anywhere.

You have to remember that email is an asynchoronous data transfer process - once sent you have no idea if it will reach the recipient or when.

In short, no, I don't think it's possible for PHP to confirm a successful delivery to the outside. If it could, I don't doubt that it already would!
 

MrMagoo

Member
Sep 6, 2005
17
0
151
Thank you webignition, that all makes a lot more sense to me now. I'm off to try and explain to my client it can't be done like they thought it could. Cheers. :)