简体   繁体   中英

Change the Return-Path in PHP mail function

Is it possible to change the Return-Path value in emails are sending via mail() function of PHP ?

It's value is 'www-data@mydomain.com' in emails I send in my site and it causes some problems on email delivery failed process. I want to set it to my email address.

Here's the code I have tried:

$headers = 'MIME-Version: 1.0' . "\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\n";
$headers .= "Return-Path: <adminemail@yahoo.com>"."\n";
$headers .= "Errors-To: <adminemail@yahoo.com>"."\n";
// Additional headers
$headers .= "To: email1@yahoo.com <adminemail@yahoo.com>" . "\n";
$headers .= "From: adminemail@yahoo.com <adminemail@yahoo.com>";
// Mail it
mail('email1@yahoo.com', 'test', 'salam', $headers, "f");

You can set reply to & return path into headers as below

$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'Return-Path: webmaster@example.com'

OR as the fifth parameter to adjust the return path

mail($to, $subject, $message, $headers, "-f email@wherever.com");

where email@wherever.com should be replaced by your mail.

The issue is mail format requires headers to use \\r\\n line endings... not \\n , the trick with that is some servers will accept both (convert them for you and it seems to work magically) while others will consider those without \\r\\n endings to be invalid and basically ignore all your headers. So try instead:

$headers = "MIME-Version: 1.0\r\n".
   "Content-type: text/html; charset=utf-8\r\n".
   "Return-Path: adminemail@yahoo.com";
mail ("email1@yahoo.com","test","salam",$headers);

By the way, return-path expects an RFC1123 mailbox (without the angle brackets, just the email address) ... not sure why you'd want to set return-path as in your example since it's the same as the from address (so superfluous)

Just define Return-Path

$returnPath = "-fadminemail@yahoo.com"; //-f will do the job
mail('email1@yahoo.com', 'test', 'salam', $headers, $returnPath);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM