在 python 中解析具有错误 mime 类型的电子邮件

发布于 2024-12-20 23:36:40 字数 471 浏览 4 评论 0原文

我正在尝试使用 python 的 imaplib 和 email.feedparser 从 gmail 收件箱中获取附件。该电子邮件是由外部方生成并发送给我们的,因此我无法控制它。

问题是我尝试解析的消息有 msg.get_content_maintype() 返回 'text' 而不是 'multitype'。因此,uuencoded 附件与邮件的其余部分连接在一起,我没有找到一种简单的方法将其从 email.message.Message 中提取出来。

有什么想法如何从此类电子邮件中提取附件吗?

如果有任何帮助,该电子邮件中包含“由 Microsoft MimeOLE V6.00.3790.4862 制作”。 Thunderbird 在渲染这封电子邮件时也遇到了麻烦,无法确定它是否有附件。否则,邮件在 Outlook 和 Gmail Web 客户端中看起来正常。

I am trying to use python's imaplib and email.feedparser to grab an attachment out of a gmail inbox. The email is generated by an external party and sent to us, so I have no control over it.

The trouble is that the message I am trying to parse has msg.get_content_maintype() return 'text' instead of 'multitype'. As a result the uuencoded attachment gets concatenated with the rest of the message and I don't see an ease way to pull it out of email.message.Message.

Any ideas how I can extract the attachment out of such an email?

If it is any help, the email has 'Produced By Microsoft MimeOLE V6.00.3790.4862' in it. Thunderbird also had trouble rendering this email and wasn't able to figure out that it had an attachment. Otherwise, the message looks ok in Outlook and Gmail web client.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヅ她的身影、若隐若现 2024-12-27 23:36:40

正如 email.parser.FeedParser 文档 所说,您可以使用_factory 参数来提供您自己的 Message 类。您可以放置​​从 email.message.Message 派生的自己的类,如果此消息是在“Produced By Microsoft MimeOLE ...blablabla”中撰写的,则该类将用正确的内容类型替换消息内容类型。

我认为可以肯定的是,在这种特殊情况下,消息将始终是多部分的。

As email.parser.FeedParser documentation says you can use _factory parameter to provide your own Message class. You could put your own class derived from email.message.Message that will replace message contenttype with the correct one if this message was composed in "Produced By Microsoft MimeOLE ...blablabla".

I think it is safe to expect that the messagesin this particular case will always be multipart.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文