Get starred messages from GMail using IMAP4 and python

后端 未结 2 689
挽巷
挽巷 2021-02-15 23:57

I found many dummy info about working with IMAP, but I didn\'t understand how to use it for my purposes. I found how I can get ALL messages from mailbox and ALL SEEN messages, b

2条回答
  •  故里飘歌
    2021-02-16 00:09

    from imap_tools import MailBox, AND
    
    with MailBox('imap.mail.com').login('test@mail.com', 'pwd', 'INBOX') as mailbox:
    
        # get list of subjects of flagged emails from INBOX folder
        subjects = [msg.subject for msg in mailbox.fetch(A(flagged=True))]
    
        # set flag on emails from INBOX that html contains  tag
        flags = [imap_tools.MailMessageFlags.FLAGGED]
        mailbox.flag([m.uid for m in mailbox.fetch() if '' in m.html], flags, True)
    
        # print flags for all emails from INBOX  
        for msg in mailbox.fetch(): print(msg.date, msg.flags)
    

    My external lib: https://github.com/ikvk/imap_tools

提交回复
热议问题