【问题标题】:inline/embedded Image attachment is not visible in mail send by PHP Swift Mailer内联/嵌入式图像附件在 PHP Swift Mailer 发送的邮件中不可见
【发布时间】:2019-11-07 05:58:45
【问题描述】:

我正在发送带有附件的图像,该附件必须显示为内嵌图像。

我不知道它有什么问题。当我编写邮件程序时,我不记得这是一个问题

这是邮件内图片的标题

--_=_swift_1561449240_8047151997d755eb732462545a29a594_=_
Content-Type: image/png; name=logo.png
Content-Transfer-Encoding: base64
X-Attachment-Id: logo
Content-ID: <logo>
Content-Disposition: inline; filename=logo.png

这是发送带有附件的电子邮件的功能

public function send(MailInterface $mail, array $attachments = array())
    {
        /* @var SwiftMail $swiftMail */
        $swiftMail = new SwiftMail(
            $mail->getSubject(),
            $mail->getBody(),
            "text/html"
        );

        foreach ($attachments as $contentId => $contentPath) {
            if (null != $contentPath) {

                // prepare content to be send as an attachment
                $attachment = SwiftAttachment::fromPath($contentPath);
                $attachment->setDisposition("inline");
                $attachment->getHeaders()->addTextHeader("Content-ID", "<" . $contentId . ">");
                $attachment->getHeaders()->addTextHeader("X-Attachment-Id", $contentId);

                // add the attachment to the mail
                $swiftMail->embed($attachment);
            }
        }

    /* ... etc ... */

在模板中,我将源设置为内容 ID,如下所示:

<img src="cid:<logo>">

【问题讨论】:

    标签: php email email-attachments swiftmailer


    【解决方案1】:

    如果您只想将电子邮件中的图像显示为内联,请使用 Swift_Image

    $cid = $mail->embed(\Swift_Image::fromPath('/path/to/image'));
    $emailView->assign('cid', $cid);
    

    在模板处:

    <img src="{cid}" />
    

    但要小心: 它不适用于同时发生的 $mail->attach()。我还不知道为什么。

    【讨论】:

      猜你喜欢
      • 2011-07-14
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多