28

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");
SteAp
  • 11,853
  • 10
  • 53
  • 88
hd.
  • 17,596
  • 46
  • 115
  • 165

3 Answers3

49

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.

SteAp
  • 11,853
  • 10
  • 53
  • 88
GBD
  • 15,847
  • 2
  • 46
  • 50
  • I try this ,but the return-path doesn't change. I check the full-header of recieve mail and it is still the same. – hd. Sep 21 '12 at 11:04
  • 4
    Try to place space after -f and check. "-f email@gmail.com" – GBD Sep 21 '12 at 11:39
  • 5
    I think `\r\n` needs to be in double quotes, not single. – Ian Dunn Apr 05 '13 at 17:28
  • 8
    The -f option worked for me. Setting the Return-Path header (as I was trying before arriving on this answer) was not working, it was overridden by the system. – Ale Nov 24 '14 at 08:32
  • 4
    In my case `-femail@wherever.com` working just fine without any space or anything. – sourav c. Jun 29 '15 at 06:05
  • 3
    I think it's considered incorrect to set the Return-Path header. The Receiving MTA sets the Return-Path based on the Envelope.From. PHPMailer stopped allowing the Return-Path to be set in the message headers for that reason. See https://stackoverflow.com/a/1247155/450127 – Ian Dunn Aug 21 '16 at 19:14
5

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)

SteAp
  • 11,853
  • 10
  • 53
  • 88
Rudu
  • 15,682
  • 4
  • 47
  • 63
  • This just saved my day. My return path was being ignored and defaulting back to www-data.. changed to from `\n` to `\r\n` and it's working as expected. – But those new buttons though.. Sep 12 '14 at 00:11
  • 3
    -1 Because this doesn't answer the question and gives non-working code. You have a random `"f"` as the last parameter for `mail()`. There is an `-f` flag. Just inputting `f` is a mistake. –  Aug 28 '15 at 19:04
4

Just define Return-Path

$returnPath = "-fadminemail@yahoo.com"; //-f will do the job
mail('email1@yahoo.com', 'test', 'salam', $headers, $returnPath);
Patrick W
  • 1,485
  • 4
  • 19
  • 27
Fadi
  • 267
  • 2
  • 6