php mail()标头阻止电子邮件发送

时间:2016-01-03 02:16:43

标签: php email smtp html-email email-headers

我不会撒谎。我真的不明白我在这个邮件功能的标题中使用的一些代码。我一直试图自己修复它,一些代码是从其他论坛帖子中复制的。

$email$subject$msg变量都很好,当我之前用这3个变量测试时,电子邮件正在发送。然后我添加了一个“发件人”部分的标题,发件人名称已修复(但电子邮件进入我的垃圾文件夹 - 烦人)。

现在我正在尝试向$msg添加一些html标签,并根据其他论坛帖子使用了我$header变量中的最后两行,但这只是停止发送电子邮件所有。请告诉我如何解决这个问题。

$headers = "From: website <donotreply@website.com>" . PHP_EOL .
"BCC: customer1@hotmail.com" . PHP_EOL . 
"MIME-Version: 1.0 \r\n" . PHP_EOL . 
"Content-Type: text/html; charset=UTF-8' \r\n";

$email = "SomeEmail@hotmail.com";
$subject = "Weekly Newsletter";
mail($email, $subject, $msg, $headers);

谢谢大家,我评论部分提醒我发布错误。它说:

警告:mail():在第45行的/path/publishnewsletter.php的additional_header中找到多个或格式错误的换行符

2 个答案:

答案 0 :(得分:4)

"MIME-Version: 1.0 \r\n" . PHP_EOL .新行太多了。根本不要使用PHP_EOL;使用\r\n,只使用一次。

charset后还有一个额外的单引号。

$headers =
    "From: website <donotreply@website.com>\r\n" .
    "BCC: customer1@hotmail.com\r\n" . 
    "MIME-Version: 1.0\r\n" . 
    "Content-Type: text/html; charset=UTF-8";

答案 1 :(得分:-1)

看看这可能有帮助

  • 清理标题。 additional_headers参数中没有多个换行符。这些被视为“多个或格式错误的换行符”:\r\r, \r\0, \r\n\r\n, \n\n, \n\0
  • 仅对标题使用additional_headers。电子邮件消息(多部分或不具有ir,没有附件等)属于message参数,而不是标题。
  • 请勿使用PHP_EOL

PHP安全漏洞报告:https://bugs.php.net/bug.php?id=68776
C Code diff如何修复:http://git.php.net/?p=php-src.git;a=blobdiff;f=ext/standard/mail.c;h=448013a472a3466245e64b1cb37a9d1b0f7c007e;hp=1ebc8fecb7ef4c266a341cdc701f0686d6482242;hb=9d168b863e007c4e15ebe4d2eecabdf8b0582e30;hpb=eee8b6c33fc968ef8c496db8fb54e8c9d9d5a8f9

相关问题