下载附件并将邮件标记为看不见

时间:2013-04-22 14:03:54

标签: python email imap

我想从未读邮件中下载附件,但也不希望将邮件标记为已查看

以下代码有效,但目前将邮件设置为看到

尝试'(BODY.PEEK[HEADER])',但后来邮件下载停止了。

import upload,checkFileAtServer,sha1sum,email, getpass, imaplib, os
detach_dir = '.'
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login('myaccount@gmail.com','password')
m.select("inbox")

resp, items = m.search(None, "(UNSEEN)")
items = items[0].split()

for emailid in items:
  #resp, data = m.fetch(emailid, '(BODY.PEEK[HEADER])')
  resp, data = m.fetch(emailid, "(RFC822)")
  email_body = data[0][1]
  mail = email.message_from_string(email_body)
  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()
        sha1sum = sha1sum.calculateSHA1(att_path)
        print type(sha1sum)
        responseFromServer = checkFileAtServer.responseFromServer(sha1sum)
        if(responseFromServer == "NOT_CHECKED"):
            upload.uploadToSecureServer('root','root',att_path,att_path)

任何人都可以指导我错过了什么?

感谢。

1 个答案:

答案 0 :(得分:1)

如果您不想将邮件标记为\Seen,请不要调用STORE IMAP命令,也不要使用FETCH项记录的项目导致隐含标记为(是的,RFC822BODY[]的别名,导致邮件被标记为已读。)