简体   繁体   English

PHP邮件标头无法正常工作

[英]PHP mail headers is not working properly

I'm sending mails using the PHP mail() function. 我正在使用PHP mail()函数发送邮件。 The mail headers is not working properly. 邮件标头无法正常工作。

$charset = mb_detect_encoding($message);

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: '.$from . "\r\n";    
$headers .= 'Content-type: text/html; charset='.$charset  . "\r\n"; 
$headers .= 'Reply-To: '.$from . "\r\n";
$headers .= 'X-Mailer: php';

In the above code, the only first line is parsed and the later 4 lines are showing in the message body. 在上面的代码中,解析了唯一的第一行,后面的4行显示在消息体中。 "From" was not set. “来自”没有设定。

$charset = mb_detect_encoding($message);

$headers  = 'From: '.$from . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset='.$charset  . "\r\n";
$headers .= 'Reply-To: '.$from . "\r\n";
$headers .= 'X-Mailer: php';

In the above code, "From" and "MIME" lines are parsed correctly, but the later 3 lines are showing in the message body. 在上面的代码中,“From”和“MIME”行被正确解析,但后面的3行显示在消息正文中。

GMail is receiving it correctly. GMail正确接收它。

Have you tried to use \\n only instead of \\r\\n ? 您是否尝试仅使用\\n而不是\\r\\n

http://php.net/manual/en/function.mail.php http://php.net/manual/en/function.mail.php

Note: 注意:

If messages are not received, try using a LF (\\n) only. 如果未收到消息,请尝试仅使用LF(\\ n)。 Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). 一些Unix邮件传输代理(最明显的是»qmail)自动替换CRLF的LF(如果使用CRLF,则会导致CR加倍)。 This should be a last resort, as it does not comply with » RFC 2822. 这应该是最后的手段,因为它不符合»RFC 2822。

You may use the code below to easy change of end of line in email: 您可以使用以下代码轻松更改电子邮件中的行尾:

$EEOL = "\n";
$headers  = 'From: '.$from . $EEOL;
$headers .= 'MIME-Version: 1.0' . $EEOL;

You should consider using an email class library instead of the regular mail() function in php. 您应该考虑使用电子邮件类库而不是php中的常规mail()函数。 I personally like the free SwiftMailer because of its simplicity and great functions for attachments etc. 我个人喜欢免费的SwiftMailer,因为它简单,附件功能强大等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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