【问题标题】:Can't display an embedded image in an email in outlook 2013 sent using using python smtplib/email无法在使用 python smtplib/email 发送的 Outlook 2013 电子邮件中显示嵌入图像
【发布时间】:2013-10-15 22:06:21
【问题描述】:

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

Embed picture in email

Sending Multipart html emails which contain embedded images

creating a MIME email template with images to send with 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()

我已经检查了 Outlook 中我能想到的所有下载和安全设置,它们都很好。我还将发件人添加到安全发件人列表中。我可以使用 Outlook 中的常规工具接收使用嵌入图像创建的其他电子邮件。从我一直在做的阅读和查看收到的电子邮件的来源来看,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>

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

【问题讨论】:

    标签: python email outlook smtplib


    【解决方案1】:

    编辑 2:

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

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

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

    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--
    

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

    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==--
    

    编辑 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 ....
    

    我看到了不同之处:Content-ID - 大写 D

    【讨论】:

    • 我在其他示例中看到了您在上面发布的内容,我只是不知道如何到达那里。我要使用的示例是在 perl 中(我也不知道),并且手动进行了很多 base64 编码和边界生成。我认为我走在正确的道路上只是因为 gmail 工作正常,但我的来源看起来不像你发布的那样。感谢您对 Content-ID 的更正,我之前更改了它,但忘记在我的帖子中进行更改。
    • 谢谢,删除纯文本部分对我有用。对于发现这一点的其他人,我对上述代码所做的唯一更改是删除“text”和“part1”,然后注释掉 msg.attach part1。之后图像和 html 内容显示正常(这是我真正需要的)
    猜你喜欢
    • 2021-12-10
    • 2020-09-19
    • 1970-01-01
    • 2018-10-27
    • 2012-12-08
    • 2019-04-18
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多