Python - imaplib,解析收件箱和处理电子邮件

问题描述 投票:0回答:0

我正在使用连接到 Outlook/Office365 邮箱的 imaplib 来阅读具有特定主题的看不见的电子邮件。然后我需要处理附件(下载它们)并将消息标记为“已读”,这样它就不会再次下载相同的附件。

我找到了 oAuth 并连接到邮箱,但我不确定如何使用 imaplib.IMAP4_SSL 对象来实际处理收件箱中的特定电子邮件消息

def connectMailbox():
    token = oAuthConnect.acquireToken()

    mailserver = 'outlook.office365.com'
    mailBox = '[email protected]'
    imapport = 993
    imap = imaplib.IMAP4_SSL(mailserver,imapport)
    imap.debug = 4
    imap.authenticate('XOAUTH2', lambda x: generate_auth_string(mailBox, token['access_token']))
    
    if token:
        print("Auth successful.")
        return imap

def parseInbox(imap):
    m = imap
    status, messages = m.select("INBOX")
    print(m)
    print(status)
    print(messages)

if __name__ == "__main__":
    connectMailbox()

imap 对象作为参数发送给“parseInbox()”函数

输出:

('OK', [b'(\\HasNoChildren) "/" Archive', b'(\\HasChildren) "/" Calendar', b'(\\HasNoChildren) "/" Calendar/Birthdays', b'(\\HasNoChildren) "/" "Calendar/United States holidays"', b'(\\HasChildren) "/" Contacts', b'(\\HasChildren) "/" "Conversation History"', b'(\\HasNoChildren \\Trash) "/" "Deleted Items"', b'(\\HasNoChildren \\Drafts) "/" Drafts', b'(\\Marked \\HasNoChildren) "/" INBOX', b'(\\HasNoChildren) "/" Journal', b'(\\HasNoChildren \\Junk) "/" "Junk Email"', b'(\\HasNoChildren) "/" Notes', b'(\\HasNoChildren) "/" Outbox', b'(\\HasNoChildren \\Sent) "/" "Sent Items"', b'(\\HasNoChildren) "/" Tasks'])
  34:23.18 > b'LOPC3 SELECT INBOX'
  34:23.28 < b'* 3 EXISTS'
  34:23.28 < b'* 0 RECENT'
  34:23.28 < b'* FLAGS (\\Seen \\Answered \\Flagged \\Deleted \\Draft $MDNSent)'
  34:23.28 < b'* OK [PERMANENTFLAGS (\\Seen \\Answered \\Flagged \\Deleted \\Draft $MDNSent)] Permanent flags'
  34:23.28 < b'* OK [UNSEEN 1] Is the first unseen message'
  34:23.28 < b'* OK [UIDVALIDITY 14] UIDVALIDITY value'
  34:23.28 < b'* OK [UIDNEXT 31] The next unique identifier value'
  34:23.28 < b'LOPC3 OK [READ-WRITE] SELECT completed.'
<imaplib.IMAP4_SSL object at 0x000002BEDC10B350>
OK
[b'3']

所以 [b'3'] 是收件箱,里面有 3 条未读消息。 有谁能解释我应该如何使用这个对象?

python python-3.x office365 imaplib
© www.soinside.com 2019 - 2024. All rights reserved.