簡體   English   中英

僅獲取新電子郵件 imaplib 和 python

[英]Get only NEW Emails imaplib and python

這是一個更大項目的一小部分。 我只需要獲取未讀的電子郵件並解析它們的標題。 如何修改以下腳本以僅獲取未讀電子郵件?

conn = imaplib.IMAP4_SSL(imap_server)
conn.login(imap_user, imap_password)

status, messages = conn.select('INBOX')    

if status != "OK":
    print "Incorrect mail box"
    exit()

print messages

像這樣的事情會成功。

conn = imaplib.IMAP4_SSL(imap_server)

try:
    (retcode, capabilities) = conn.login(imap_user, imap_password)
except:
    print sys.exc_info()[1]
    sys.exit(1)

conn.select(readonly=1) # Select inbox or default namespace
(retcode, messages) = conn.search(None, '(UNSEEN)')
if retcode == 'OK':
    for num in messages[0].split(' '):
        print 'Processing :', message
        typ, data = conn.fetch(num,'(RFC822)')
        msg = email.message_from_string(data[0][1])
        typ, data = conn.store(num,'-FLAGS','\\Seen')
        if ret == 'OK':
            print data,'\n',30*'-'
            print msg

conn.close()

這里還有一個重復的問題 - 查找自我上次使用 python imaplib2 檢查后添加到 imap 郵箱的新消息?

兩個有用的函數可用於檢索檢測到的新郵件的正文和附件(參考: 如何在 python 中使用 imaplib 獲取電子郵件正文? )-

def getMsgs(servername="myimapserverfqdn"):
  usernm = getpass.getuser()
  passwd = getpass.getpass()
  subject = 'Your SSL Certificate'
  conn = imaplib.IMAP4_SSL(servername)
  conn.login(usernm,passwd)
  conn.select('Inbox')
  typ, data = conn.search(None,'(UNSEEN SUBJECT "%s")' % subject)
  for num in data[0].split():
    typ, data = conn.fetch(num,'(RFC822)')
    msg = email.message_from_string(data[0][1])
    typ, data = conn.store(num,'-FLAGS','\\Seen')
    yield msg

def getAttachment(msg,check):
  for part in msg.walk():
    if part.get_content_type() == 'application/octet-stream':
      if check(part.get_filename()):
        return part.get_payload(decode=1)

PS:如果你在2020年python 2.7死后路過:用email.message_from_string(data[0][1])替換email.message_from_bytes(data[0][1])

上面的答案實際上不再起作用,或者可能永遠不會起作用,但我對其進行了修改,因此它只返回看不見的消息,它曾經給出:錯誤無法解析提取命令或類似的東西,這是一個有效的代碼:

mail = imaplib.IMAP4_SSL('imap.gmail.com')
(retcode, capabilities) = mail.login('email','pass')
mail.list()
mail.select('inbox')

n=0
(retcode, messages) = mail.search(None, '(UNSEEN)')
if retcode == 'OK':

   for num in messages[0].split() :
      print 'Processing '
      n=n+1
      typ, data = mail.fetch(num,'(RFC822)')
      for response_part in data:
         if isinstance(response_part, tuple):
             original = email.message_from_string(response_part[1])

             print original['From']
             print original['Subject']
             typ, data = mail.store(num,'+FLAGS','\\Seen')

print n

我認為錯誤來自messages[0].split(' ')但上面的代碼應該可以正常工作。

另外,請注意將消息標記為已讀的+FLAGS而不是-FLAGS

編輯 2020:如果您在 2020 年在 python 2.7 死后路過:將email.message_from_string(data[0][1])替換為email.message_from_bytes(data[0][1])

original = email.message_from_string(response_part[1])

需要更改為:

original = email.message_from_bytes(response_part[1])

您可以使用 imap_tools 包: https ://pypi.org/project/imap-tools/

from imap_tools import MailBox, AND
with MailBox('imap.mail.com').login('test@mail.com', 'password', 'INBOX') as mailbox:
    # get unseen emails from INBOX folder
    for msg in mailbox.fetch(AND(seen=False)):
        print(msg.date, len(msg.html or msg.text))

我已經設法使用 Gmail 使其正常工作:

import datetime
import email
import imaplib
import mailbox


EMAIL_ACCOUNT = "your@gmail.com"
PASSWORD = "your password"

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(EMAIL_ACCOUNT, PASSWORD)
mail.list()
mail.select('inbox')
result, data = mail.uid('search', None, "UNSEEN") # (ALL/UNSEEN)
i = len(data[0].split())

for x in range(i):
    latest_email_uid = data[0].split()[x]
    result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
    # result, email_data = conn.store(num,'-FLAGS','\\Seen') 
    # this might work to set flag to seen, if it doesn't already
    raw_email = email_data[0][1]
    raw_email_string = raw_email.decode('utf-8')
    email_message = email.message_from_string(raw_email_string)

    # Header Details
    date_tuple = email.utils.parsedate_tz(email_message['Date'])
    if date_tuple:
        local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
        local_message_date = "%s" %(str(local_date.strftime("%a, %d %b %Y %H:%M:%S")))
    email_from = str(email.header.make_header(email.header.decode_header(email_message['From'])))
    email_to = str(email.header.make_header(email.header.decode_header(email_message['To'])))
    subject = str(email.header.make_header(email.header.decode_header(email_message['Subject'])))

    # Body details
    for part in email_message.walk():
        if part.get_content_type() == "text/plain":
            body = part.get_payload(decode=True)
            file_name = "email_" + str(x) + ".txt"
            output_file = open(file_name, 'w')
            output_file.write("From: %s\nTo: %s\nDate: %s\nSubject: %s\n\nBody: \n\n%s" %(email_from, email_to,local_message_date, subject, body.decode('utf-8')))
            output_file.close()
        else:
            continue

我不喜歡現有的解決方案,所以我決定為我的 email 發件人創建一個名為Red Box的姊妹庫。

以下是如何獲取新消息並處理它們的示例:

from redbox import EmailBox

# Create email box instance
box = EmailBox(
    host="imap.example.com", 
    port=993,
    username="me@example.com",
    password="<PASSWORD>"
)

# Select an email folder
inbox = box["INBOX"]

# Search and process messages
for msg in inbox.search(unseen=True):

    # Process the message
    print(msg.headers)
    print(msg.from_)
    print(msg.to)
    print(msg.subject)
    print(msg.text_body)
    print(msg.html_body)

    # Set the message as read/seen
    msg.read()

如果您需要復雜的邏輯操作,還有一種查詢語言 如果需要,您還可以輕松訪問消息的各個部分

安裝:

pip install redbox

鏈接:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM