[Python] Reading emails with imap_tools getting wrong sort-output

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    i want to read the emails from an email-account from the newsest to the oldest mail in the inbox using the following code:

    from imap_tools import MailBox
    import os
    import sys
    from dotenv import load_dotenv

    path = os.path.abspath(os.path.dirname(sys.argv[0]))
    fn = os.path.join(path, ".env")
    load_dotenv(fn)
    LOGIN_EMAIL = os.environ.get("LOGIN_EMAIL")
    LOGIN_PW = os.environ.get("LOGIN_PW")
    SERVER = os.environ.get("SERVER")

    with MailBox(SERVER).login(LOGIN_EMAIL, LOGIN_PW) as mailbox:
    for msg in mailbox.fetch(reverse=True):
    print(msg.date, msg.subject)
    input("Press!")


    When i run this code i get this output for the first 3 emails:

    (test) C:\DEVNEU\Fiverr2025\TRY\franksrn>python test.py
    2025-02-17 17:14:02+01:00 Bestellung
    Press!
    2024-12-17 17:14:02+01:00 Bestellung Franklin Apotheke e.K.
    Press!
    2025-02-10 12:38:46+01:00 Bestellnr. 4500606968
    Press!


    But when i look in the email-account i have different mails as the newest first 3 in the inbox:

    [​IMG]

    How can i get the newest mails with the correct sorting from the email-inbox?

    Continue reading...

Compartilhe esta Página