PHP邮件标头无法正常工作

时间:2014-06-08 05:51:17

标签: php email

我正在尝试使用PHP创建电子邮件,但是在构建标题时遇到了问题。

我希望From为admin@example.com,其他标题设置如下所示。

问题在于,当我发送此电子邮件时,"来自"电子邮件客户端中的部分显示为:

  

" admin@example.com rnReply-To:admin@example.com rnMIME-Version:1.0 rnContent-Type:text / html"

这也无意中打破了电子邮件的其余部分,因为它没有将其识别为html。

// email headers
$headers = 'From: admin@example.com \r\n';
$headers .= 'Reply-To: admin@example.com \r\n';
$headers .= 'MIME-Version: 1.0 \r\n';
$headers .= 'Content-Type: text/html; charset=UTF-8 \r\n';

关于如何修复这些标头的任何想法?感谢。

3 个答案:

答案 0 :(得分:4)

更改以下内容:

$headers = 'From: admin@example.com \r\n';
$headers .= 'Reply-To: admin@example.com \r\n';
$headers .= 'MIME-Version: 1.0 \r\n';
$headers .= 'Content-Type: text/html; charset=UTF-8 \r\n';

对此:

$headers = "From: admin@example.com \r\n";
$headers .= "Reply-To: admin@example.com \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
$headers .= "Content-Type: text/html; charset=UTF-8 \r\n";

因为\r\n应该在""(双引号)。

答案 1 :(得分:2)

尝试:

$headers = 'From: admin@example.com' ."\n"; /* note double quote here*/
$headers .= 'Reply-To: admin@example.com' . "\n";/* note double quote here*/
$headers .= 'MIME-Version: 1.0' . "\n";/* note double quote here*/
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";/* here too*/

编辑(尚未评论) 不知道你为电子邮件编写脚本有多少经验,但是我必须在工作时为我们的新闻通讯创建一个系统,并想出一些能够通过spamblock等的好标题,这需要一些修修补补。 您可能希望查看可以使用的邮件框架,如Zend。

你也试试" \ r \ n"对于mime类型,它对我来说已经有一段时间但我记得混合结果与" \ n,\ r \ n和\ r \ n \ r \ n"的组合

答案 2 :(得分:1)

结果\ r \ n需要双引号,单引号不起作用。