0

我想从 Unread Messages 下载附件,但也不希望这些消息被标记为Seen

以下代码有效,但当前将邮件设置为已查看

试过了'(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)

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

谢谢。

4

1 回答 1

1

如果您不想将消息标记为\Seen,请不要调用STOREIMAP 命令并且不要使用FETCH记录在案的能够导致隐式标记的项目(是的,这是导致消息被标记RFC822的别名BODY[]如阅读)。

于 2013-04-22T15:43:06.383 回答