从IMAP帐户中提取pdf附件 - python 3.5.2

时间:2016-10-04 01:31:00

标签: python email imap

好的,所以我试图将发送到特定帐户的pdf附件保存到特定的网络文件夹,但我已经卡在附件部分。我已经获得了以下代码来引入看不见的消息,但我不确定如何获取"部分"保持完整。如果我能弄清楚如何保持电子邮件的完整性,我想我可以想出来。我从来没有过去"让它走路"输出。此帐户中的所有测试电子邮件都包含pdf附件。提前致谢。

import imaplib
import email
import regex
import re

user = 'some_user'
password = 'gimmeAllyerMoney'

server = imaplib.IMAP4_SSL('mail.itsstillmonday.com', '993')
server.login(user, password)
server.select('inbox')

msg_ids=[]
resp, messages = server.search(None, 'UNSEEN')
for message in messages[0].split():
        typ, data = server.fetch(message, '(RFC822)')
        msg= email.message_from_string(str(data[0][1]))
        #looking for 'Content-Type: application/pdf
        for part in msg.walk():
                print("Made it to walk")
                if part.is_multipart():
                        print("made it to multipart")
                if part.get_content_maintype() ==  'application/pdf':
                        print("made it to content")

1 个答案:

答案 0 :(得分:0)

您可以使用part.get_content_type()获取完整内容类型,使用part.get_payload()获取有效内容,如下所示:

for part in msg.walk():
    if part.get_content_type() == 'application/pdf':
        # When decode=True, get_payload will return None if part.is_multipart()
        # and the decoded content otherwise.
        payload = part.get_payload(decode=True)

        # Default filename can be passed as an argument to get_filename()
        filename = part.get_filename()

        # Save the file.
        if payload and filename:
            with open(filename, 'wb') as f:
                f.write(payload)

请注意,正如tripleee指出的那样,对于内容类型为" application / pdf"你有:

>>> part.get_content_type()
"application/pdf"
>>> part.get_content_maintype()
"application"
>>> part.get_content_subtype()
"pdf"