Download a specific email from Gmail using Python

前端 未结 3 1651
不知归路
不知归路 2021-02-07 11:13

Can someone help me customize an existing code sample?

I can see from the following article how to connect to gmail and download content, but I can\'t figure out how to

3条回答
  •  爱一瞬间的悲伤
    2021-02-07 11:52

    import datetime as dt
    from imap_tools import MailBox, Q
    date = dt.date.today() - dt.timedelta(days=5)
    with MailBox('imap.mail.com').login('test@mail.com', 'password', 'INBOX') as mailbox:
        for msg in mailbox.fetch(Q(from_='Alerts@foobank.com', date_gte=date)):
            sent_time = msg.date
            body = msg.text or msg.html
            for att in msg.attachments:
                att.filename         # str: 'cat.jpg'
                att.content_type     # str: 'image/jpeg'
                att.payload          # bytes: b'\xff\xd8\xff\xe0\'
    

    *Note that there is no imap search criteria "with attachments"

    https://github.com/ikvk/imap_tools

提交回复
热议问题