2013-10-08 90 views
3

我已經看過其他幾個職位,包括髮送:無法在Outlook 2013中的郵件顯示嵌入式圖像使用使用python的smtplib /電子郵件

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

回答

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: <[email protected]> 

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:[email protected]"> 
... 

------=_NextPart_000_0009_01CEC44B.4C788080 
Content-Type: image/png; 
    name="caddiigg.png" 
Content-Transfer-Encoding: base64 
Content-ID: <[email protected]> 

iVBORw0KGgoAAAANSUhEUg .... 

我看到了區別:Content-ID - 一個大寫D

+0

我已經看到了你在其他例子中上面張貼我只是不知道如何到達那裏。我要走的例子是perl(我不太清楚),並且手動執行了很多base64編碼和邊界生成。我想我只是在正確的道路上,因爲Gmail的工作正常,但我的來源看起來不像你發佈的東西。感謝您對Content-ID的更正,我之前對其進行了更改,但忘記在我的文章中進行更改。 – Matty

+1

謝謝,刪除純文本部分爲我做了詭計。對於其他人的發現,我對上述代碼所作的唯一更改是刪除「text」和「part1」,然後註釋掉msg.attach part1。之後,圖像和HTML內容顯示正常(這是我真正需要的) – Matty

+0

我使用這個但不工作... https://docs.google.com/file/d/0Bx7i5tBqPWWWZUpMM3JVajRKdEk/edit?usp=drivesdk – user00000341

相關問題