更改 PHP 邮件功能中的返回路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12446039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:35:48  来源:igfitidea点击:

Change the Return-Path in PHP mail function

emailphp

提问by hd.

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

是否可以更改通过 PHP 的 mail() 函数发送的电子邮件中的返回路径值?

It's value is '[email protected]' 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.

它的值是我在网站上发送的电子邮件中的“[email protected]”,它会导致电子邮件传递失败过程中出现一些问题。我想将其设置为我的电子邮件地址。

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: <[email protected]>"."\n";
$headers .= "Errors-To: <[email protected]>"."\n";
// Additional headers
$headers .= "To: [email protected] <[email protected]>" . "\n";
$headers .= "From: [email protected] <[email protected]>";
// Mail it
mail('[email protected]', 'test', 'salam', $headers, "f");

回答by GBD

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

您可以将回复和返回路径设置为标题,如下所示

$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'Return-Path: [email protected]'

OR as the fifth parameter to adjust the return path

OR 作为第五个参数调整返回路径

mail($to, $subject, $message, $headers, "-f [email protected]");

where [email protected] should be replaced by your mail.

其中 [email protected] 应替换为您的邮件。

回答by Rudu

The issue is mail format requires headers to use \r\nline 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\nendings to be invalid and basically ignore all your headers. So try instead:

问题是邮件格式要求标题使用\r\n行结尾......不是\n,其中的技巧是一些服务器会同时接受(为您转换它们并且它似乎神奇地工作)而其他人会认为没有\r\n结尾的那些是无效的并且基本上忽略所有标题。所以试试吧:

$headers = "MIME-Version: 1.0\r\n".
   "Content-type: text/html; charset=utf-8\r\n".
   "Return-Path: [email protected]";
mail ("[email protected]","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)

顺便说一下,return-path 需要一个 RFC1123 邮箱(没有尖括号,只有电子邮件地址)......不知道为什么你想在你的例子中设置 return-path,因为它与发件人地址相同(太多余了)

回答by Fadi

Just define Return-Path

只需定义返回路径

$returnPath = "[email protected]"; //-f will do the job
mail('[email protected]', 'test', 'salam', $headers, $returnPath);