Gmail不会使用PHP呈现HTML电子邮件

时间:2014-02-19 09:29:21

标签: php html email

我一直试图让gmail呈现我发送给它的电子邮件的html内容,但它只是给了我电子邮件的完整文本。当我发送电子邮件到yahoo或我在comcast.net的电子邮件地址时,我看到html电子邮件部分没有问题。 Gmail会渲染整个内容,包括边界字符串。这是我的代码:

//  Create a boundary string. It must be unique
//  so we use the MD5 algorithm to generate a random hash.
$random_hash = md5(date('r', time()));
//  Define the headers we want passed. Note that they are separated with \r\n.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: \"" . $replyToName . "\" <" . $replyTo . ">\r\nReply-To: " . $replyTo . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
//  Add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-" . $random_hash ."\"";
$headers .= "\r\n";
//This is the plain-text part of the e-mail. This is what the recipient will see if they can only receive text-only e-mails.
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit

<?php echo $plainMsg; ?>

<?php echo $mainSite; ?>
______________________________________________________

This e-mail is intended for the eyes of the recipient only. If you are not the
intended recipient, please send an e-mail to <?php echo $replyTo; ?> to inform us of
the mistake, and please destroy any and all copies of this e-mail.

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
<head>
<title>Untitled Document</title>
</head>
<table width="100%" border="0">
<tr>
<td>
<table width="100%" border="0" bgcolor="#ADD8E6">
<tr>
<td><img src="<?php echo $mainLink; ?>/images/BF.gif" width="95" height="65"></td>
<td valign="top"><img src="<?php echo $imageLink; ?>" width="<?php echo $imageWidth; ?>" height="<?php echo $imageHeight; ?>" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<font face="Arial, Helvetica, sans-serif" size="2">
<?php echo $mailMsg; ?>
</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>
<table width="100%" border="0">
<tr>
<td>
<p>
<font face="Arial, Helvetica, sans-serif" size="2">
<a href="<?php echo $mainLink; ?>"><?php echo $mainSite; ?></a>
</font>
<br />
______________________________________________________
<br />
<br />
<font face="Arial, Helvetica, sans-serif" size="1">
This e-mail is intended for the eyes of the recipient only. If you are not the intended recipient, please send an e-mail to <a href="mailto:<?php echo $replyTo; ?>"><?php echo $replyTo; ?></a> to inform us of the mistake, and please destroy any and all copies of this e-mail.
</font>
</p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="100%" border="0" bgcolor="#ADD8E6">
<tr>
<td align="center">
<font size="-2">All content, images, and data &#0169; Bright Futures 2009-2014.
<br />
All rights reserved.</font>
</td>
</tr>
</table>
</td>
</tr>
</table>

--PHP-alt-<?php echo $random_hash; ?> --

当我发送此电子邮件时,我看到整个邮件未编码。它显示了消息中的边界字符串和html标记。当我在gmail中转到“Show Original”时,我得到以下内容:

Delivered-To: gsg1175@gmail.com
Received: by 10.182.23.6 with SMTP id i6csp32095obf;
        Wed, 19 Feb 2014 01:03:00 -0800 (PST)
X-Received: by 10.68.106.130 with SMTP id gu2mr924626pbb.59.1392800580001;
        Wed, 19 Feb 2014 01:03:00 -0800 (PST)
Return-Path: <gsg1175@p3plcpnl0157.prod.phx3.secureserver.net>
Received: from m1plded02-04.prod.mesa1.secureserver.net (m1plded02-04.prod.mesa1.secureserver.net. [64.202.189.17])
        by mx.google.com with ESMTP id ri10si3237317pbc.297.2014.02.19.01.02.59
        for <gsg1175@gmail.com>;
        Wed, 19 Feb 2014 01:02:59 -0800 (PST)
    Received-SPF: pass (google.com: domain of gsg1175@p3plcpnl0157.prod.phx3.secureserver.net designates 64.202.189.17 as permitted sender) client-ip=64.202.189.17;
Authentication-Results: mx.google.com;
       spf=pass (google.com: domain of gsg1175@p3plcpnl0157.prod.phx3.secureserver.net designates 64.202.189.17 as permitted sender) smtp.mail=gsg1175@p3plcpnl0157.prod.phx3.secureserver.net
Received: from p3plcpnl0157.prod.phx3.secureserver.net ([184.168.200.188])
    by m1plded02-04.prod.mesa1.secureserver.net with : DED :
    id U92x1n01E44PGPL0192x1U; Wed, 19 Feb 2014 02:02:59 -0700
Received: from gsg1175 by p3plcpnl0157.prod.phx3.secureserver.net with local (Exim 4.82)
    (envelope-from <gsg1175@p3plcpnl0157.prod.phx3.secureserver.net>)
    id 1WG33R-001P1W-8P; Wed, 19 Feb 2014 02:02:57 -0700
To: gsg1175@gmail.com,g.greenwell@comcast.net
Subject: Test E-mail
X-PHP-Script: www.yellowcas.com/sendemail.php for 107.4.20.247
MIME-Version: 1.0
From: "Gregory Greenwell" <admin@yellowcas.com>
Reply-To: admin@yellowcas.com
CC: looking4unabq@yahoo.com
X-Mailer: PHP/5.4.23
Content-Type: multipart/alternative; boundary="PHP-alt-8d0c53f7f76522e3a03197894d7e89fe"
Message-Id: <E1WG33R-001P1W-8P@p3plcpnl0157.prod.phx3.secureserver.net>
Date: Wed, 19 Feb 2014 02:02:57 -0700
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - p3plcpnl0157.prod.phx3.secureserver.net
X-AntiAbuse: Original Domain - gmail.com
X-AntiAbuse: Originator/Caller UID/GID - [267295 956] / [47 12]
X-AntiAbuse: Sender Address Domain - p3plcpnl0157.prod.phx3.secureserver.net
X-Get-Message-Sender-Via: p3plcpnl0157.prod.phx3.secureserver.net: authenticated_id: gsg1175/from_h
X-Source: 
X-Source-Args: /usr/sbin/proxyexec -q -d -s /var/run/proxyexec/cagefs.sock/socket /bin/cagefs.server 
X-Source-Dir: yellowcas.com:/public_html


--PHP-alt-8d0c53f7f76522e3a03197894d7e89fe 
Content-Type: text/plain; charset="utf-8" 
Content-Transfer-Encoding: 7bit 

We will keep doing this until we get it right!

Why won't this work?

WWW.YELLOWCAS.COM
______________________________________________________

This e-mail is intended for the eyes of the recipient only. If you are not the intended recipient, please send an e-mail to admin@yellowcas.com to inform us of the mistake, and please destroy any and all copies of this e-mail.

--PHP-alt-8d0c53f7f76522e3a03197894d7e89fe 
Content-Type: text/html; charset="utf-8" 
Content-Transfer-Encoding: 7bit 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
<head>
<title>Untitled Document</title>
</head>
<table width="100%" border="0">
<tr>
<td>
<table width="100%" border="0" bgcolor="#ADD8E6">
<tr>
<td><img src="https://www.yellowcas.com/images/BF.gif" width="95" height="65"></td>
<td valign="top"><img src="https://www.yellowcas.com/images/brightfutures70.gif" width="520" height="60" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<font face="Arial, Helvetica, sans-serif" size="2">
We will keep doing this <b>until we get it right!</b>

Why won't this work?
</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>
<table width="100%" border="0">
<tr>
<td>
<p>
<font face="Arial, Helvetica, sans-serif" size="2">
<a href="https://www.yellowcas.com">WWW.YELLOWCAS.COM</a>
</font>
<br />
______________________________________________________
<br />
<br />
<font face="Arial, Helvetica, sans-serif" size="1">
This e-mail is intended for the eyes of the recipient only. If you are not the intended recipient, please send an e-mail to <a href="mailto:admin@yellowcas.com">admin@yellowcas.com</a> to inform us of the mistake, and please destroy any and all copies of this e-mail.
</font>
</p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="100%" border="0" bgcolor="#ADD8E6">
<tr>
<td align="center">
<font size="-2">All content, images, and data &#0169; Bright Futures 2009-2014.
<br />
All rights reserved.
</font>
</td>
</tr>
</table>
</td>
</tr>
</table>

--PHP-alt-8d0c53f7f76522e3a03197894d7e89fe-- 

我为此感到抱歉这么久,但我无法弄清楚为什么gmail没有将html显示为html。我正在使用php mail()函数发送电子邮件。我看过的每个其他电子邮件客户端都可以很好地呈现html,但是gmail只是在电子邮件中显示底层代码。如果任何人有任何想法,我将非常感谢听到他们。感谢您抽出宝贵时间查看我的代码。

2 个答案:

答案 0 :(得分:1)

我注意到您正在使用\r\n。 Gmail不再呈现它。如果将其替换为\n,则应在gmail处呈现。

之前我发现它是因为我吓坏了gmail不再渲染它了。经过一项研究,我结束了this blog,解决了我的“小问题”。

答案 1 :(得分:0)

以下是我为系统解决此问题所采取的措施。

第一:经过一些研究以及反复试验后,我发现Gmail和Thunderbird都会在标题行的末尾对齐额外的字符。 &#34; \ R&#34;算作额外的字符,空格也是如此。我删除了所有&#34; \ r&#34;整个事情中的字符是肯定的,因为我的代码编辑程序有时会在编辑时添加它们(我在不同的操作系统之间切换很多)。为此,您可以使用输出缓冲区。

 ob_start();

 // all of your output here like normal

 $output = str_replace("\r","",ob_get_clean());
 echo $output;  // now your output has no "\r" characters

第二:我仔细检查了我的MIME兼容性。我碰巧在一个地方错过了一个分号,但在另一个地方有一个额外的分号,我在几个地方也有额外的空格。您有不必要的(有时可能是不允许的)引号。这是一个更好的方法来做标题,以便您可以清楚地看到可能有额外字符的位置。通过使用数组构建标题并使用&#34; \ n&#34;进行插入,我保证没有&#34; \ r&#34;两端的字符或空格。从技术上讲,您不必将变量放入括号{},但为了清晰起见,我总是这样做。

$replyToName = trim($replyToName);
$replyTo = trim($replyTo);

$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "From: {$replyToName} <{$replyTo}>";
$headers[] = "Reply-To: {$replyTo}";
$headers[] = "X-Mailer: PHP/".phpversion();
$headers[] = "Content-Type: multipart/alternative;boundary=PHP-alt-{$random_hash}";
$headers = implode("\n", $headers)."\n";

第三:与您一样,我遇到了DNS问题,Gmail通过添加X-AntiAbuse标头来响应。你不想看到那些,因为它们意味着你离垃圾邮件文件夹只有一步之遥。您应该转到http://www.microsoft.com/mscorp/safety/content/technologies/senderid/wizard/default.aspx并按照向导为DNS创建新的SPF行,然后将向导创建的内容复制并粘贴到DNS条目中。如果您不知道这意味着什么,请致电您的网络托管服务商的技术支持,他们应该引导您完成它。

您当前的DNS:

v=spf1 a mx ptr include:secureserver.net ~all

它应该是这样的:

v=spf1 a mx ip4:107.4.20.247 ptr:secureserver.net a:secureserver.net include:secureserver.net ~all  

通过设置&#34; ptr&#34;和&#34; a&#34;您的电子邮件服务器(Secureserver.net根据您在问题中粘贴的内容)的值,它允许Gmail和其他接收服务器知道您的电子邮件服务器可以为您发送电子邮件。包含您网站的特定IP地址会使更多电子邮件客户认为您不是垃圾邮件发送者。

顺便说一句,你真的不需要为你的边界字符串做一个md5哈希,而且与你的评论不同,它并不一定是&#34;唯一的,&#34;只是没有找到电子邮件的正文。只需输入一串绝对不会出现在任何电子邮件中的字母和数字,您就可以了。我的是&#34; BOUNDARY456BREAK&#34;。没有人会在电子邮件中写下这个。