[英]Multipart/mixed email attachments not showing up, but only in Windows 10 Mail
我通過Python email
/ smtplib
發送的電子郵件有一個奇怪的問題。
我正在嘗試撰寫一封電子郵件:
MIME結構設置如下:
multipart/mixed
multipart/alternative
text/plain
multipart/related
text/html
image/png - inline
application/pdf - attachment
這似乎在我測試的每個郵件客戶端上運行良好{在Android上運行BlueMail,iOS郵件客戶端,Roundcube}, 除了 Windows 10郵件客戶端。 出於某種原因,Windows 10內置郵件客戶端似乎顯示內聯圖像就好了,但沒有顯示其他附件的痕跡。
我在互聯網上找到的有限信息表明這是Windows 10郵件客戶端的一個錯誤,但我個人收到了這個客戶端的其他電子郵件,包括內聯和附加的附件,顯示得很好 - 所以那里顯然是某種解決方法/替代消息結構有效。
我的問題是: 我如何以不同方式格式化此消息,以便它能在所有相關郵件客戶端中正確顯示?
我在Python中編寫這樣的電子郵件:
message = MIMEMultipart("mixed")
message["From"] = ...
.
.
.
bodyText = "..."
bodyHTML = "..."
mailFrom = "..."
targetEmail = "..."
imageContent = ...
messageBody = MIMEMultipart("alternative")
messageBody.attach(MIMEText(bodyText, "plain"))
messageBodyHTML = MIMEMultipart("related")
messageBodyHTML.attach(MIMEText(bodyHTML, "html"))
messageImage = MIMEImage(imageContent)
messageImage.add_header("Content-Disposition", 'inline; filename="..."')
messageImage.add_header("Content-ID", "<id used in html body>")
messageBodyHTML.attach(messageImage)
messageBody.attach(messageBodyHTML)
message.attach(messageBody)
attachment = MIMEApplication(fileContent, Name=fileName)
attachment.add_header("Content-Disposition", 'attachment; filename="..."')
message.attach(attachment)
self.smtplibSession.sendmail(mailSource, targetEmail, message.as_string())
更新:這是來自Windows 10郵件的消息數據(通過“保存”功能輸出 - 無法查看我可以找到的原始消息原始數據...)
MIME-Version: 1.0
Date: Thu, 30 May 2019 17:45:28 +0200
From: xxxxx <xxxxx>
Subject: xxxxx
Thread-Topic: xxxxx
To: "xxxxx" <xxxxx>
Content-Type: multipart/related;
boundary="_5D6C043C-FD42-42F9-B0E0-841DBFBA96D5_"
--_5D6C043C-FD42-42F9-B0E0-841DBFBA96D5_
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="utf-8"
<center><img src=3D"cid:embedded-image" alt=...
--_5D6C043C-FD42-42F9-B0E0-841DBFBA96D5_
Content-Type: image/png; name="embedded-image.png"
Content-ID: <embedded-image>
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="embedded-image.png"
iVBORw0KGgoAAAAN...
--_5D6C043C-FD42-42F9-B0E0-841DBFBA96D5_--
我不確定這是否是從應用程序保存電子郵件的結果,或者這是應用程序實際存儲的內容,但似乎Windows 10 Mail應用程序正在刪除multipart/related
節之外的所有內容 - 這是,它只采取選擇的alternative
而不是存儲任何其他東西。
為了比較,我發現並導出了一個正確顯示的電子郵件,帶有圖像,html和附件,但格式似乎更簡單 - 該電子郵件僅包含帶有text/html
的multipart/mixed
層和application/pdf
附件。 該電子郵件使用HTML中引用的外部圖像,而不是將其嵌入到郵件中 - 我希望避免在外部托管每封電子郵件中的圖像。
與您不同,附件文件沒有問題,而是在顯示內嵌圖像時出現問題( Windows 10 Mail 16005.11629.20174.0
)。
遺憾的是,正確處理MIME郵件中的非標准方法是一項預計具有良好電子郵件客戶端的功能。 顯然Windows 10 Mail還不是那么“好”。
我建議你使用的結構是:
multipart/mixed
├─── multipart/related
│ ├─── multipart/alternative
│ │ ├─── text/plain
│ │ └─── text/html
│ └─── image/png - inline image
└─── application/pdf - attachment
我在以下客戶端中對此結構沒有任何問題。
因此,請嘗試以下代碼,看看它是否適合您。
message = MIMEMultipart("mixed")
message["From"] = ...
.
.
.
bodyText = "..."
bodyHTML = "..."
mailFrom = "..."
targetEmail = "..."
imageContent = ...
fileContent = ...
relatedBody = MIMEMultipart("related")
messageBody = MIMEMultipart("alternative")
messageBody.attach(MIMEText(bodyText, "plain"))
messageBody.attach(MIMEText(bodyHTML, "html"))
relatedBody.attach(messageBody)
messageImage = MIMEImage(imageContent)
messageImage.add_header("Content-Disposition", 'inline; filename="..."')
messageImage.add_header("Content-ID", "<id used in html body>")
relatedBody.attach(messageImage)
message.attach(relatedBody)
attachment = MIMEApplication(fileContent)
attachment.add_header("Content-Disposition", 'attachment; filename="..."')
message.attach(attachment)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.