无法在使用python smtplib / email发送的Outlook 2013中的电子邮件中显示嵌入式图像

Mat*_*tty 5 python email outlook smtplib

我看过其他几篇文章,包括:

将图片嵌入电子邮件

发送包含嵌入式图像的多部分html电子邮件

创建带有图像的MIME电子邮件模板以使用python / django发送

这些以及smtplib和电子邮件的python文档使我离我很近。我正在使用下面的代码来创建嵌入了简单jpg的电子邮件。如果我将电子邮件发送给gmail,它将很好地显示嵌入的图像,但Outlook 2013无法显示。

import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

From = ''
To = ''

msg = MIMEMultipart()
msg['Subject'] = 'image test message'
msg['From'] = From
msg['To'] = To


text = 'This is sample text from me'
html = '''
<html>
    <head>
        <title> this is a test title </title>
    </head>
    <body>
        <p> Test me <br>
        Another line <br>
        This is the image you were looking for <img src="cid:test_image"><br>
        This will teach you not to click on links in strange <a href="http://purple.com">emails</a>
        </p>
    </body>
</html>
'''

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

img_data = open('image.jpg', 'rb').read()
img = MIMEImage(img_data, 'jpeg')
img.add_header('Content-Id', '<test_image>')
msg.attach(img)

s = smtplib.SMTP('localhost')
s.sendmail(From, To, msg.as_string())
s.quit()
Run Code Online (Sandbox Code Playgroud)

我已经检查了Outlook中所有可以想到的下载和安全设置,它们都很好。我还将发件人添加到了安全发件人的列表中。我可以在Outlook Fine中接收使用常规工具使用嵌入式图像创建的其他电子邮件。根据我一直在阅读的内容,并查看收到的电子邮件的来源,看来Outlook不知道在哪里找到图像。也没有与此电子邮件关联的附件。右键单击电子邮件并查看源代码时,会得到以下内容。

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"><title> this is a test title </title>
    </head>
    <body>
        <p> Test me <br>
        Another line <br>
        This is the image you were looking for <img src="cid:test_image"><br>
        This will teach you not to click on links in strange <a href="http://purple.com">emails</a>
        </p>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我目前认为它与内容类型有关,或者我只是搞砸了代码。我认为代码是正确的,因为gmail可以正常显示图像,并且当我将其从gmail转发到Outlook时,转发的消息也可以正常显示。

Use*_*ser 2

编辑2:

首先尝试不使用简单文本版本:

Content-Type: multipart/related;
    boundary="----=_NextPart_000_0009_01CEC44B.4C788080"
Run Code Online (Sandbox Code Playgroud)

当显示图像时,请尝试以下操作并将元素放入替代部分,如下所示:

Subject: ...
From: ...
To: ...
Content-Type: multipart/related;
    type="multipart/alternative";
    boundary="----=_NextPart_000_0009_01CEC44B.4C788080"

------=_NextPart_000_0009_01CEC44B.4C788080
Content-Type: multipart/alternative;
    boundary="----=_NextPart_001_000A_01CEC44B.4C788080"


------=_NextPart_001_000A_01CEC44B.4C788080
Content-Type: text/plain;
    charset="ISO-8859-15"
Content-Transfer-Encoding: quoted-printable

My Simple text

------=_NextPart_001_000A_01CEC44B.4C788080
Content-Type: text/html;
    charset="ISO-8859-15"
Content-Transfer-Encoding: quoted-printable

My HTML Text

------=_NextPart_001_000A_01CEC44B.4C788080--

------=_NextPart_000_0009_01CEC44B.4C788080
Content-Type: image/png;
    name="caddiigg.png"
Content-Transfer-Encoding: base64
Content-ID: <38F81D2D49CB42B2AD8F93F5CF01BCA1@SKNB>

iVBORw0KGgoAAAANSUhEUgAAAxcAAAH0CAIAAADADUduAAAgAElEQVR4nEy8adP02H3ex0+TFymZ
5JAzw01OpazZOVS2SjkvYpHzzELasiuOK4tLkhVJMcWZu7E0loN96x07cPZzsPR2P8+QlPOh8gL9
DFn1L


------=_NextPart_000_0009_01CEC44B.4C788080--
Run Code Online (Sandbox Code Playgroud)

目前源代码显示了这样一封电子邮件:

Content-Type: multipart/mixed; boundary="===============0661849094=="
MIME-Version: 1.0
Subject: image test message
From: 
To: 

--===============0661849094==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

This is sample text from me
--===============0661849094==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit


<html>
    <head>
        <title> this is a test title </title>
    </head>
    <body>
        <p> Test me <br>
        Another line <br>
        This is the image you were looking for <img src="cid:test_image"><br>
        This will teach you not to click on links in strange <a href="http://purple.com">emails</a>
        </p>
    </body>
</html>

--===============0661849094==
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Id: <test_image>

YmxhYmxh
--===============0661849094==--
Run Code Online (Sandbox Code Playgroud)

编辑1:

这就是电子邮件的工作原理:

...
<BR><IMG alt=3D""=20
src=3D"cid:38F81D2D49CB42B2AD8F93F5CF01BCA1@SKNB">
...

------=_NextPart_000_0009_01CEC44B.4C788080
Content-Type: image/png;
    name="caddiigg.png"
Content-Transfer-Encoding: base64
Content-ID: <38F81D2D49CB42B2AD8F93F5CF01BCA1@SKNB>

iVBORw0KGgoAAAANSUhEUg ....
Run Code Online (Sandbox Code Playgroud)

我看到了区别:Content-ID- 大写D