2014-02-19 42 views
0

我一直试图让gmail呈现我发送给它的电子邮件的html内容,但它只是给了我电子邮件的完整文本。当我在comcast.net发送电子邮件给雅虎或我的电子邮件地址时,我看到html电子邮件部分没有任何问题。 Gmail呈现整个事物,包括边界字符串。这是我下面的代码:Gmail将不会使用PHP呈现HTML电子邮件

// 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中,我得到如下:

Delivered-To: [email protected] 
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: <[email protected]> 
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 <[email protected]>; 
     Wed, 19 Feb 2014 01:02:59 -0800 (PST) 
    Received-SPF: pass (google.com: domain of [email protected] 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 [email protected] designates 64.202.189.17 as permitted sender) [email protected] 
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 <[email protected]>) 
    id 1WG33R-001P1W-8P; Wed, 19 Feb 2014 02:02:57 -0700 
To: [email protected],[email protected] 
Subject: Test E-mail 
X-PHP-Script: www.yellowcas.com/sendemail.php for 107.4.20.247 
MIME-Version: 1.0 
From: "Gregory Greenwell" <[email protected]> 
Reply-To: [email protected] 
CC: [email protected] 
X-Mailer: PHP/5.4.23 
Content-Type: multipart/alternative; boundary="PHP-alt-8d0c53f7f76522e3a03197894d7e89fe" 
Message-Id: <[email protected]> 
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 [email protected] 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:[email protected]">[email protected]</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只是在电子邮件中显示底层代码。如果任何人有任何想法,我会非常感激听到他们。感谢您抽出时间查看我的代码。

+0

我有这个完全相同的问题。如果您的DNS条目不正确,我发现Gmail添加了X-AntiAbuse。您可以尝试http://www.microsoft.com/mscorp/safety/content/technologies/senderid/wizard/default.aspx为您的DNS条目生成正确的SPF。我正在尝试这个。如果它不起作用,我会为这个问题增加一笔赏金。如果确实有效,我会添加我所做的,作为对您问题的回答。 – miyasudokoro

回答

1

我注意到您正在使用\r\n。 Gmail不会再显示它。如果你用\n替换它,它应该在gmail上渲染。

我之前发现它,因为我吓坏了,gmail没有再渲染它。经过研究,我结束了this blog解决了我的'小'问题。

+0

我曾看过另一篇文章,并提供了一个指向那个确切页面的链接。我尝试过,但由于某些原因,这对我没有任何作用。我会再试一次并发布我的结果。感谢您的答复。我感谢任何帮助,我可以得到。 –

+0

我从代码中删除了所有\ r,并且gmail仍然给我相同的结果。这令我很沮丧。 –

+0

我没有发现任何证据,特别是\ r字符,而是\ n之前的任何额外字符,包括空格。 – miyasudokoro

0

下面是我为解决我的系统的这个问题所做的事情。

第一:经过一些研究以及试验和错误之后,我发现Gmail和Thunderbird都会在标题行末尾添加额外的字符。 “\ r”作为一个额外的字符,正如空格。为了确保我删除了所有“\ r”字符,因为我的代码编辑器程序有时会在编辑时添加它们(我在不同的操作系统间切换很多)。为此,您可以使用输出缓冲区。

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兼容性。我碰巧在一个地方错过了一个分号,但在另一个地方有一个额外的分号,而且我在几个地方也有额外的空格。你有不必要的(有时可能不允许)引号。这是更好的方法来做标题,这样你就可以清楚地看到可能有多余字符的地方。通过使用数组构建标题并使用“\ n”进行imploding,我保证在末尾没有“\ r”字符或空格。您在技术上不必将变量放入括号{},但我始终这样做是为了清晰起见。

$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 

由 “PTR” 和 “a” 的值设置为您的电子邮件服务器(secureserver.net根据你的问题粘贴的内容),它可以让Gmail和其他接收服务器知道你的电子邮件服务器可以为你发送电子邮件。包括您网站的特定IP地址可以让更多的电子邮件客户端相信您不是垃圾邮件发送者。

顺便说一句,你并不需要为你的边界字符串做一个md5散列,而且不像你的评论,它不必是“唯一的”,只是没有在电子邮件的正文中找到。只需输入一串字母和数字,绝对不会出现在任何电子邮件中,而且您很好。我的是“BOUNDARY456BREAK”。没有人会写在电子邮件中。

相关问题