簡體   English   中英

無法在使用 python smtplib/電子郵件發送的 outlook 2013 中的 email 中顯示嵌入圖像

[英]Can't display an embedded image in an email in outlook 2013 sent using using python smtplib/email

我看過其他幾個帖子,包括:

在 email 中嵌入圖片

發送包含嵌入圖像的多部分 html 電子郵件

創建一個 MIME email 模板,其中包含要與 python / django 一起發送的圖像

這些以及用於 smtplib 和 email 的 python 文檔讓我很接近。 我正在使用下面的代碼創建一個 email,其中嵌入了一個簡單的 jpg。 如果我將 email 發送到 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 中的常規工具接收使用嵌入圖像創建的其他電子郵件。 從我一直在做的閱讀和查看收到的 email 的來源來看,outlook 似乎不知道在哪里可以找到圖像。 也沒有與此 email 關聯的附件。 下面是我右鍵單擊 email 並查看源代碼時得到的結果。

<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 時,轉發的消息顯示正常。

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

這里有一個非常好的帖子,有多種解決這個問題方法。 我能夠發送帶有 HTML、Excel 附件和嵌入圖像的 email。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM