【问题标题】:mail() php doesn't send in html formatmail() php 不以 html 格式发送
【发布时间】:2016-11-29 18:16:13
【问题描述】:

我尝试了这里已经回答的问题(thisthisthis 和其他人......),但没有任何工作......我有这个功能可以发送电子邮件:

function send_email($para, $assunto, $msg){
    $to      = $para;
    $subject = $assunto;

    $message = '<html><body>';
    $message .= str_replace("\\r\\n","<br/>",$msg);
    $message .= '</body></html>';

    $headers = 'From: email@realemail.com' . "\r\n";
    $headers .= 'Reply-To: email@realemail.com' . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";

    return mail($to, $subject, $message, $headers);
}

我用类似的东西来称呼它:

$var = "<p><strong>...blablabla...</strong></p>";

send_email("email@email", "lalala", $var);

我可以发送电子邮件,但它不是 html 格式的(它包含 html 标签,HTMLBODY 除外)。我做错了什么?

编辑 1: 邮件显示是这样的(发送的内容完全一样,除了HTMLBODYTAG):

BEGIN
<p><strong>...blablabla</strong></p>
END

当我收到完整的电子邮件(如 this 教程)时,显示该电子邮件是使用 TAGS html、正文等发送的... 一封电子邮件:

Delivered-To: myemail@gmail.com
...
To: myemail@gmail.com
Subject: aaaaaaaaaaaaaaaaaaaaa
From: sender@email.com
Reply-To: sender@email.com
X-Mailer: PHP/5.6.19
Content-type: text/html; charset=iso-8859-1
MIME-Version: 1.0
...
Date: Tue, 29 Nov 2016 15:51:48 -0200
...
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - svr1.ravehost.com.br
X-AntiAbuse: Original Domain - gmail.com
X-AntiAbuse: Originator/Caller UID/GID - [99 497] / [47 12]
X-AntiAbuse: Sender Address Domain - svr1.ravehost.com.br

<html><body>...blablabla...</body></html>

【问题讨论】:

  • 您最终能否在此处复制/粘贴带有标题和所有内容的生成的电子邮件?
  • 查看edit1 @Dragos。
  • 否则标题顺序不正确,您的代码看起来不错。可能是客户端有问题,你用的是哪个邮件客户端?
  • 尝试将Content-type 更改为Content-Type(大写T)。
  • @PMateus 电子邮件、正确的 MIME 编码和多部分消息是一场彻头彻尾的噩梦,如果你必须自己弄清楚所有的怪癖,那就太疯狂了。 :-) 使用为您执行此操作的现有邮件库,例如 PHPMailer 或类似的。

标签: php html email


【解决方案1】:

不确定这是否是一个确切的解决方案,但这解决了我在使用 Microsoft Outlook 时遇到的 html 电子邮件问题。

将标题中的所有\r\n 替换为PHP_EOL。当我在用 PHP 发送 html 电子邮件时遇到问题时,我这样做了,它似乎解决了我的特殊问题。

【讨论】:

  • PHP_EOL 会根据您使用的平台更改其值。如果你在 Windows 上运行 PHP,你会得到\r\n。如果你在 Linux 上运行它,你会得到\n
猜你喜欢
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 2012-11-08
  • 2014-12-04
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多