【问题标题】:Embedding image with swiftmailer makes gmail show html as attachment使用 swiftmailer 嵌入图像使 gmail 将 html 显示为附件
【发布时间】:2015-04-16 03:46:09
【问题描述】:

我正在尝试使用 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');

【问题讨论】:

  • 你有想过这个吗?
  • 遗憾的是没有 - 我只是链接到文件的在线版本

标签: php gmail swiftmailer


【解决方案1】:

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

省略 text/plain body,text/html 部分显示得很好。

【讨论】:

    【解决方案2】:

    消息很旧,但如果有帮助的话。

    您可以为您的消息添加附件,给它一个文件名。 文档摘录 (swiftmailer doc):

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

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

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-09-12
      • 1970-01-01
      • 2018-09-24
      • 2011-05-01
      • 2012-07-28
      • 2011-10-30
      • 2020-05-29
      • 2017-03-05
      • 2019-03-14
      相关资源
      最近更新 更多