[英]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.