简体   繁体   中英

Get emails with imap_tools with python

I want to receive all the today's email texts from a specific sender and put them into a list from outlook with imap-tools

I have made the following function but the problem is that it doesn't retrieve emails from 12:00AM - 12:00PM, is there any way to specify the hours for getting the proper messages?

def get_emails(username, password, sender):
    from imap_tools import MailBox, A

    emails = []

    with MailBox('outlook.office365.com').login(username, password, 'INBOX') as mailbox:
        for msg in mailbox.fetch(
                A(
                    A(date_gte=datetime.date.today()),  # get the today's emails
                    A(from_=sender),         # from the specific senderEmailAddress
                ),
                mark_seen = True
            ):
            if msg.subject == "Subject":
                emails.append(msg.text)
        return emails

Use date ( imap_tools documentation ):

from imap_tools import MailBox, A

def get_emails(username, password, sender):
    emails = []

    with MailBox('outlook.office365.com').login(login_name, login_password, 'INBOX') as mailbox:
        for msg in mailbox.fetch(
                A(date=datetime.date.today(), from_=sender),
                mark_seen = True
            ):
            if msg.subject == "Subject":
                emails.append(msg.text)
        return email

There is no filter by time in IMAP, only by date.

Filter by time on client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM