在 Thunderbird 中对嵌入的电子邮件图像使用 Content-ID 和 cid

Using Content-ID and cid for embedded email images in Thunderbird

我在 PHP 应用程序中生成电子邮件,该应用程序将多个文件附加到 HTML 电子邮件。有些文件是 Excel 电子表格,有些文件是需要嵌入到 HTML 中的公司徽标,因此它们默认加载时使用 Content-ID 和 cid 标识符来引用附加图像。

据我所知,我的语法是正确的,但图像永远不会内联加载(但是它们已成功附加)。

From: email@example.com
Reply-To: email@example.com
MIME-Version: 1.0
Content-type: multipart/mixed;boundary="d0f4ad49cc20d19bf96d4adf9322d567"
Message-Id: <20150421165500.0A5488021B@server>
Date: Tue, 21 Apr 2015 12:54:59 -0400 (EDT)

--d0f4ad49cc20d19bf96d4adf9322d567
Content-type: text/html; charset=utf-8
Content-transfer-encoding: 8bit

<html>
    Html message goes here, followed by email.<br/>
    <img src="cid:mylogo" />
</html>
--d0f4ad49cc20d19bf96d4adf9322d567
Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; name=excelsheet.xlsx
Content-Description: excelsheet.xlsx
Content-Disposition: attachment;
 filename="excelsheet.xlsx"; size=24712;
Content-transfer-encoding:base64

[base64 encoded string goes here.]

--b19e863e2cf66b40db1d138b7009010c
Content-Type: image/jpeg;
 name="mylogo.jpg"
Content-transfer-encoding:base64
Content-ID: <mylogo>
Content-Disposition: inline;
 filename="mylogo.jpg"; size=7579;

[base64 encoded string goes here.]

--b19e863e2cf66b40db1d138b7009010c--

有人能看出图像无法按预期嵌入的明显原因吗?

编辑

请注意,并非所有电子邮件客户端都存在这种行为。到目前为止只在 Thunderbird 中提到过。

我注意到两个问题:

  1. MIME 边界不一致。对于第一个附件,它是 d0f4ad49cc20d19bf96d4adf9322d567,然后使用 b19e863e2cf66b40db1d138b7009010c。因此,从技术上讲,第二个附件是第一个附件的“一部分”。

    如果您将所有 b19e863e2cf66b40db1d138b7009010c 替换为 d0f4ad49cc20d19bf96d4adf9322d567 Thunderbird 会正确识别图像附件。

  2. 使用 multipart/related 而不是 multipart/mixed。 (参见 RFC2387

    A multipart/related is used to indicate that each message part is a component of an aggregate whole. It is for compound objects consisting of several inter-related components - proper display cannot be achieved by individually displaying the constituent parts. The message consists of a root part (by default, the first) which reference other parts inline, which may in turn reference other parts. Message parts are commonly referenced by the "Content-ID" part header. (see Wikipedia entry for MIME multipart/related)