PHP Mail - How to specify a "Fail to" address?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Stearmandriver
Forum Newbie
Posts: 3
Joined: Sat Sep 25, 2010 9:11 pm

PHP Mail - How to specify a "Fail to" address?

Post by Stearmandriver »

Hi all,

Quick and probably stupid question here, but I've been Googling for an hour and can't find the answer - When using the PHP mail function, how do you specify the address to which a bounced message alert is sent?

I want it to be different than the sender of the message; ie. users are filling out a web form to send comments via email to a local politician, and I want successfully delivered messages to appear to come from the user (of course, since that's who's sending it)... but I want failed emails to bounce to me.

I've got it working fine where the emails are sent to the proper address, and come FROM the user's address, and reply-to the user's address... but it seems that if I don't specify a failure address, they'll probably either fail to the user, or to my server admin, right?

I find promising mentions in the php docs of -f and envelope-sender attributes in the headers section of the mail function, but the docs don't do a good job of explaining what those are or how to use them.

Any suggestions?

Thanks...
Joe
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: PHP Mail - How to specify a "Fail to" address?

Post by robnet »

Failures go to the address specified in the 'Return-Path' header.

You should be able to add this with mail() though you should checkout a more fully featured mail class/lib. They're generally much more robust and fully featured than PHP's limited mail() function.
http://swiftmailer.org
http://phpmailer.sourceforge.net


Modified eg from http://php.net/manual/en/function.mail.php

Code: Select all

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'Return-Path: webmaster@example.com' . "\r\n" ;

mail($to, $subject, $message, $headers);
I recommend testing it, I haven't..
Stearmandriver
Forum Newbie
Posts: 3
Joined: Sat Sep 25, 2010 9:11 pm

Re: PHP Mail - How to specify a "Fail to" address?

Post by Stearmandriver »

Thanks for the suggestion.

I've tried your snippet, and it doesn't throw any errors, and emails are still correctly delivered... however, when I intentionally send to a known bad address, I still don't get the bounced email notice to the email specified in the "Return-Path" header. I'll keep tweaking when I have time, and try one of your suggested mailers (thanks for those).

Thanks again...
Joe
Stearmandriver
Forum Newbie
Posts: 3
Joined: Sat Sep 25, 2010 9:11 pm

Re: PHP Mail - How to specify a "Fail to" address?

Post by Stearmandriver »

Disregard. Turns out my host system was over-ruling the Return-Path header... I checked the full headers in an email I sent through the system to myself, and Return-Path was still some generic address for my host.

I was able to force the setting of the Return-Path header by using the envelope-sender -fmyemail@sbcglobal.net

Thanks again for the help...
Post Reply