27

是否可以更改通过 PHP 的 mail() 函数发送的电子邮件中的 Return-Path 值?

在我在我的站点中发送的电子邮件中,它的值是“www-data@mydomain.com”,它会导致电子邮件传递失败过程中出现一些问题。我想将其设置为我的电子邮件地址。

这是我尝试过的代码:

$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");
4

3 回答 3

47

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

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

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

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

其中 email@wherever.com 应替换为您的邮件。

于 2012-09-16T10:12:23.613 回答
5

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

$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);

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

于 2012-10-02T18:06:34.190 回答
2

只需定义返回路径

$returnPath = "-fadminemail@yahoo.com"; //-f will do the job
mail('email1@yahoo.com', 'test', 'salam', $headers, $returnPath);
于 2019-05-27T10:40:53.877 回答