使用imaplib下载excel附件

时间:2018-01-25 14:47:22

标签: python imaplib

每天早上都有一封包含Excel附件的电子邮件。我希望能够抓住每个附件并将它们保存到同一个文件夹中。我尝试了以下代码:

Downloading multiple attachments using imaplib

https://gist.github.com/jasonrdsouza/1674794

https://gist.github.com/baali/2633554

但我一直遇到同样的错误:

initial_value must be str or None, not bytes

我认为这是因为其中一封电子邮件/附件存在问题,但我不确定如何进行问题排查。

代码:

detach_dir = 'C:/Users/myname'
m = imaplib.IMAP4_SSL("outlook.office365.com")
m.login('emailaddress@server.com','password')
m.select("INBOX")

resp, items = m.search(None, '(SUBJECT "Daily Report")')
items = items[0].split()

for emailid in items:
    resp, data = m.fetch(emailid, "(RFC822)") 
    email_body = data[0][1] 
    mail = email.message_from_string(email_body)
    #^This is where the error occurs
    temp = m.store(emailid,'+FLAGS', '\\Seen')
    m.expunge()

    if mail.get_content_maintype() != 'multipart':
        continue

    #print "["+mail["From"]+"] :" + mail["Subject"]

   for part in mail.walk():
       if part.get_content_maintype() == 'multipart':
           continue
       if part.get('Content-Disposition') is None:
           continue

       filename = part.get_filename()
       att_path = os.path.join(detach_dir, filename)

       if not os.path.isfile(att_path) :
           fp = open(att_path, 'wb')
           fp.write(part.get_payload(decode=True))
           fp.close()

这是错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-6-07c04531d5aa> in <module>()
      5     resp, data = m.fetch(emailid, "(RFC822)")
      6     email_body = data[0][1]
----> 7     mail = email.message_from_string(email_body)
      8     #This is where the error occurs
      9     temp = m.store(emailid,'+FLAGS', '\\Seen')

~\AppData\Local\Continuum\anaconda3\lib\email\__init__.py in message_from_string(s, *args, **kws)
     36     """
     37     from email.parser import Parser
---> 38     return Parser(*args, **kws).parsestr(s)
     39 
     40 def message_from_bytes(s, *args, **kws):

~\AppData\Local\Continuum\anaconda3\lib\email\parser.py in parsestr(self, text, headersonly)
     66         the file.
     67         """
---> 68         return self.parse(StringIO(text), headersonly=headersonly)
     69 
     70 

TypeError: initial_value must be str or None, not bytes

当我为不同的主题测试代码时,代码工作正常,所以我假设有一个电子邮件或附件将其搞砸了。减少搜索到1月1日以后发送的电子邮件可能会有所帮助,但我不知道如何操纵搜索两个参数:

typ, msgs = mails.search(None, '(SUBJECT "Daily Report")', 'SENTSINCE 01-JAN-2018')

1 个答案:

答案 0 :(得分:0)

将行更改为:mail = email.message_from_bytes(email_body)