Gmail显示纯文本而不是HTML

时间:2019-12-03 21:49:43

标签: php email gmail phpmailer swiftmailer

情况

我有一个脚本,可以从Gmail帐户下载电子邮件,调整内容,然后重新发送电子邮件。

问题

每当我创建带有嵌入式图像的电子邮件时,Gmail都会显示电子邮件的纯文本版本,并且嵌入式图像会像常规附件一样显示。

已测试

如果我使用其他客户端,则图像会正确显示。如果我将电子邮件从Gmail转发到其他客户端,则图像会正确显示。只要没有嵌入式图像,具有常规附件的多部分电子邮件或具有纯文本和html部分的电子邮件都可以正确显示。

电子邮件格式

据我所见,电子邮件的格式是正确的,但我已将其粘贴在下面,为简洁和隐私进行了修剪。我注意到的两件事是边界似乎设置正确,并且img src cid:与图像的Content-ID相匹配。

MIME-Version: 1.0
Content-Type: multipart/related;
 boundary="_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_"


--_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

This email has been sent to a Mailing List.  You can approve or reject it a=
t [URL here]
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
[image: noice.gif]


-----------------------
[Signature here]


--_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<p>
    This email has been sent to a Mailing List.  You can approve or reject =
it at [URL here]</p>

<hr />
<div dir=3D"ltr"><div><img src=3D"cid:7f82a31e4f084e8f0a25edd913ed3aa2@swif=
t.generated" alt=3D"noice.gif" width=3D"474" height=3D"244"><br></div><div>=
<div dir=3D"ltr" class=3D"gmail_signature" data-smartmail=3D"gmail_signatur=
e"><div dir=3D"ltr"><div><div dir=3D"ltr"><div><div dir=3D"ltr"><br><br>---=
--------------------<br>[signature here]<br></div>=
</div></div></div></div></div></div></div>


--_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_
Content-Type: image/gif; name=noice.gif
Content-Transfer-Encoding: base64
Content-ID: <7f82a31e4f084e8f0a25edd913ed3aa2@swift.generated>
Content-Disposition: inline; filename=noice.gif

R0lGODlh2gH0APcAAAgDBRGJG5ZKGsDGB8lNFJJsGkkIBs+nLgnCM4lPZODnDqdMGo4rCVGIKs5q
[... bunch of base64 encoded stuff]
BFGgAUsAEvjkAlQgq/drOjfkFKdEETNBxl3bpi5lBYEAADs=

--_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_--

PHP代码

以下是生成Mailer对象的PHP代码-该对象是Swiftmailer的包装,并用于实际发送:

$Mailer = new Mailer($subject_prepend.$this->subject);
// Simply a wrapper.  Calls $this->Message->setBody($body,'text/plain');,
// where $this->Message is an instance of Swift_Message
$Mailer->setBody($plaintext_prepend.$this->plaintext);

$html_body = $html_prepend.$this->html;

if(count($this->Attachments)){
    foreach($this->Attachments as $Attachment){
        if($Attachment->isEmbedded()){
            $image = \Swift_Image::fromPath($Attachment->getPath())->setDisposition('inline');
            $cid = $Mailer->Message->embed($image);

            //$cid = $Mailer->embedPath($Attachment->getPath());
            $html_body = str_replace('cid:'.$Attachment->cid, $cid, $html_body);
        }
        else{
            // calls $this->Message->attach(\Swift_Attachment::fromPath($path));
            $Mailer->addAttachmentPath($Attachment->getPath());
        }
    }
}

// calls $this->Message->addPart($body,'text/html');
$Mailer->addHTMLBody($html_body);

注释掉的行是我最初嵌入图像的方式,但结果相同。

-

这是不是让Gmail失去了魔力,还是电子邮件配置不当?

编辑

我设法通过PHPMailer正确发送了电子邮件。我可以看到的唯一区别是PHPMailer的开头是:

MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="b1_6hukH7nTGJu6fpr5tpXob5uQE7wXivW0oMppPNbwOi4"
Content-Transfer-Encoding: 8bit

Swiftmailer的起始位置

MIME-Version: 1.0
Content-Type: multipart/related; boundary="_=_swift_1575416351_99d22ee774049152f712bc5ae65340fb_=_"

ie:PHPMailer使用multipart/alternative而不是multipart/related,并设置Content-Transfer-Encoding

此外,SwiftMailer电子邮件的所有文本部分都是“ Content-Transfer-Encoding:quoted-printable”,而PHPMailer并未设置该标题。

0 个答案:

没有答案