[英]Multipart/mixed email attachments not showing up, but only in Windows 10 Mail
Having a weird problem with emails I am sending out via Python email
/ smtplib
. 我通过Python email
/ smtplib
发送的电子邮件有一个奇怪的问题。
I am attempting to compose an email with: 我正在尝试撰写一封电子邮件:
The MIME structure is setup like this: MIME结构设置如下:
multipart/mixed
multipart/alternative
text/plain
multipart/related
text/html
image/png - inline
application/pdf - attachment
This seems to work fine on every mail client I've tested {BlueMail on Android, iOS mail client, Roundcube} except for the Windows 10 mail client. 这似乎在我测试的每个邮件客户端上运行良好{在Android上运行BlueMail,iOS邮件客户端,Roundcube}, 除了 Windows 10邮件客户端。 For some reason, the Windows 10 built-in mail client seems to show the inline image just fine, but shows no trace of the other attachment. 出于某种原因,Windows 10内置邮件客户端似乎显示内联图像就好了,但没有显示其他附件的痕迹。
The limited information I have been able to find on the internet points to this being a bug with the Windows 10 mail client, but I have personally received other emails in this client with both inline and attached attachments, which are displayed just fine - so there obviously is some sort of workaround / alternative message structure that works. 我在互联网上找到的有限信息表明这是Windows 10邮件客户端的一个错误,但我个人收到了这个客户端的其他电子邮件,包括内联和附加的附件,显示得很好 - 所以那里显然是某种解决方法/替代消息结构有效。
My question is thus: How can I format this message differently so that it will show up properly in all relevant mail clients? 我的问题是: 我如何以不同方式格式化此消息,以便它能在所有相关邮件客户端中正确显示?
I am composing the email like this, in Python: 我在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())
Update: Here's the message data from Windows 10 mail (as output via the "save" feature - there's no way to view the original message raw data that I can find...) 更新:这是来自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_--
I'm not sure if this is a result of saving the email from the app, or this is what the app is actually storing, but it seems that the Windows 10 Mail app is cutting out everything outside the multipart/related
stanza - that is, it's only taking the chosen alternative
and not storing anything else. 我不确定这是否是从应用程序保存电子邮件的结果,或者这是应用程序实际存储的内容,但似乎Windows 10 Mail应用程序正在删除multipart/related
节之外的所有内容 - 这是,它只采取选择的alternative
而不是存储任何其他东西。
For comparison, I've found and exported an email that displayed properly, with an image, html, and attachment, but the format seems to be a lot simpler - that email consisted only of a multipart/mixed
layer with text/html
and an application/pdf
attachment. 为了比较,我发现并导出了一个正确显示的电子邮件,带有图像,html和附件,但格式似乎更简单 - 该电子邮件仅包含带有text/html
的multipart/mixed
层和application/pdf
附件。 That email used an external image referenced in the HTML, instead of embedding it in the message - I would like to avoid hosting the images in each email externally. 该电子邮件使用HTML中引用的外部图像,而不是将其嵌入到邮件中 - 我希望避免在外部托管每封电子邮件中的图像。
Unlike you, there was no problem with the attachment file, instead I've had problems in displaying inline images ( Windows 10 Mail 16005.11629.20174.0
). 与您不同,附件文件没有问题,而是在显示内嵌图像时出现问题( Windows 10 Mail 16005.11629.20174.0
)。
Unfortunately, handling non-standard approaches in MIME messages correctly is a feature that is expected to have good email clients. 遗憾的是,正确处理MIME邮件中的非标准方法是一项预计具有良好电子邮件客户端的功能。 Apparently Windows 10 Mail is not as "good" yet. 显然Windows 10 Mail还不是那么“好”。
The structure I recommend you to use is: 我建议你使用的结构是:
multipart/mixed
├─── multipart/related
│ ├─── multipart/alternative
│ │ ├─── text/plain
│ │ └─── text/html
│ └─── image/png - inline image
└─── application/pdf - attachment
I've had no problems with this structure in the following clients. 我在以下客户端中对此结构没有任何问题。
So, give the following code a try to see if it works for you. 因此,请尝试以下代码,看看它是否适合您。
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.