我需要在电子邮件中嵌入图像。我该怎么做?
我既不想使用第三方工具,也不想对特定于语言的答案感兴趣(但是如果您想知道的话,它就是PHP)。
我只对生成的电子邮件正文的格式感兴趣。
最佳答案
如您所知,作为电子邮件传递的所有内容都必须文本化。
您必须创建包含多部分/ mime消息的电子邮件。
如果要添加物理图像,则该图像必须使用base 64编码并分配了Content-ID(cid)。如果是URL,则<img />
标记就足够了(图像的URL必须链接到Source ID)。
典型的电子邮件示例如下所示:
From: foo1atbar.net
To: foo2atbar.net
Subject: A simple example
Mime-Version: 1.0
Content-Type: multipart/related; boundary="boundary-example"; type="text/html"
--boundary-example
Content-Type: text/html; charset="US-ASCII"
... text of the HTML document, which might contain a URI
referencing a resource in another body part, for example
through a statement such as:
<IMG SRC="cid:foo4atfoo1atbar.net" ALT="IETF logo">
--boundary-example
Content-Location: CID:somethingatelse ; this header is disregarded
Content-ID: <foo4atfoo1atbar.net>
Content-Type: IMAGE/GIF
Content-Transfer-Encoding: BASE64
R0lGODlhGAGgAPEAAP/////ZRaCgoAAAACH+PUNv
cHlyaWdodCAoQykgMTk5LiBVbmF1dGhvcml6ZWQgZHV
wbGljYXRpb24gcHJvaGliaXRlZC4A etc...
--boundary-example--
如您所见,
Content-ID: <foo4atfoo1atbar.net>
ID与<IMG>
处的SRC="cid:foo4atfoo1atbar.net"
匹配。这样,客户端浏览器会将您的图像呈现为内容而不是附件。希望这可以帮助。