为什么python的电子邮件解析器找不到这个多部分MIME电子邮件的所有部分?



我正在尝试使用Python的电子邮件模块处理简单的多部分MIME电子邮件。但是,由于某种原因我不明白,我无法走完电子邮件的所有部分 - 由于某种原因,应用程序/pdf 被排除在外。

失败操作示例:

import email    
msgstring = '''See bottom of post'''
msg = email.message_from_string(msgstring)
has_pdf_attached = False
for part in msg.walk():
    print (part.get_content_type())
    if part.get_content_type() == 'application/pdf':
        payload = part.get_payload(decode=True)
        if '%PDF-' in payload:
            has_pdf_attached = True
print(has_pdf_attached)

输出(请注意,部分的print中没有最终的"应用程序/pdf"部分):

multipart/alternative
text/plain
text/html
False

消息本身,截图以显示重要位:

--_=_swift_v4_145618772756cba94f8fcc2_=_
Content-type: multipart/alternative; boundary="----------=_1456187728-18401-69"
This is a multi-part message in MIME format...
------------=_1456187728-18401-69
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
A bunch of content here
foobar
barfoo
etc
------------=_1456187728-18401-69
Content-Type: text/html; charset="utf-8"
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
<html><body>
    <p>HTML version of content</p>
</body></html>
------------=_1456187728-18401-69--
--_=_swift_v4_145618772756cba94f8fcc2_=_
Content-Type: application/pdf; name test.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename test.pdf
A_Big_Long_Base64_Enconded_PDF_File_foofoofoofoofoofoofoofoo
JVBER0OUMyNzc0ODFDODAwMTI+IF0KL0RvY0NoZWNrFADSFsfsaFdsafsdaf
dHhyZWYKMzE4MjkKJSVFT0YKFDSFDSFdsfdsfdsfdsfdsfdsfdsfdsfdsfds
--_=_swift_v4_145618772756cba94f8fcc2_=_--

那我做错了什么呢?我注意到"检测到"的部分都在第一个"部分"中,由神秘的--_=_swift_v4_145618772756cba94f8fcc2_=_--包裹。我认为这是相关的,但是谷歌和SO搜索失败了,所以我在这里。

在标准的Stack Overflow体验中,我所要做的就是公开提出问题,然后简单的答案就会在我眼前显现出来。

我的msgstring不包含整个原始电子邮件 - 我使用的 IMAP 库配置错误,并且正在切断邮件的主标题。 --_=_swift_v4_145618772756cba94f8fcc2_=_确实是一个边界 - 整个多部分消息的主要边界。

当我msgstring输入实际的完整消息时,它就像一个魅力。

把这个愚蠢的问题归结为不太了解MIME格式。

最新更新