我需要在电子邮件中嵌入图像。我该怎么做?
我不想使用第三方工具,也不对特定语言的答案感兴趣(但它是 PHP,如果你想知道的话)。
我只对生成的电子邮件正文的格式感兴趣。
最佳答案
如您所知,作为电子邮件传递的所有内容都必须文本化。
- 您必须创建包含多部分/mime 消息的电子邮件。
- 如果您要添加物理图片,则该图片必须采用 base 64 编码并分配一个 Content-ID (cid)。如果是 URL,那么
<img />
tag 就足够了(图片的 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"
.这样,客户端浏览器会将您的图像呈现为内容而不是附件。
希望这会有所帮助。
关于php - 如何在电子邮件中嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4312687/