在 python 中解析具有错误 mime 类型的电子邮件
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 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 ownMessage
class. You could put your own class derived fromemail.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.