使用swiftmailer嵌入图像使gmail显示html作为附件


Embedding image with swiftmailer makes gmail show html as attachment

我正试图用Swiftmailer创建一封html电子邮件,并在html位中嵌入一个图像。如果我只是用纯文本和html版本发送邮件,html版本显示得很好,当然会有一个破损的图像图标。不过,如果我嵌入图像,我只看到纯文本版本,html版本和图像都显示为附件。

这种方法看起来有什么问题吗?

电子邮件标题:

Message-ID: <10c312442e249148aa9e87d70681885c@swift.generated>
Date: Sun, 15 Feb 2015 16:04:55 +0100
Subject: Subject here
From: Fastaval <email@xyz.dk>
To: email@example.com
MIME-Version: 1.0
Content-Type: multipart/related;
 boundary="_=_swift_v4_1424012695_eef1c60fde0ddd3f8a9ad82190b115f8_=_"

纯文本标题:

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

Html标头:

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

嵌入式图像头:

--_=_swift_v4_1424012695_eef1c60fde0ddd3f8a9ad82190b115f8_=_
Content-Type: image/jpeg; name=Banner15.jpg
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=Banner15.jpg
Content-ID: <8e75618e41588e7e5b6953f0c319e262@swift.generated>

带有图像的html位:

<div><img alt=3D"banner" src=3D"cid:8e75618e41588e7e5=
b6953f0c319e262@swift.generated"/></div>

我正在生成这样的电子邮件:

$this->_message = Swift_Message::newInstance()
    ->setFrom($from)
    ->setTo($to)
    ->setSubject($subject)
    ->setBody($message, 'text/plain');
$html = '<div><img alt="banner" src="banner-src"/></div>';
$html = str_replace('banner-src', $this->_message->embed(Swift_Image::fromPath('Banner15.jpg')), $html);
$this->_message->addPart($html, 'text/html');

我遇到了同样的问题,最终做了这个:

去掉text/plain正文,text/html部分显示得很好。

该消息很旧,但如果可以帮助的话。

你可以对你的邮件做一个附件,给它一个文件名。文件摘录(swiftmailer文档):

$message->attach(Swift_Attachment::fromPath('/path/to/image.jpg')->setFilename('cool.jpg'));

您可以在html中调用img的src中的文件名。

希望它能帮助到别人。