[Python] Error using imap-tools with an email address containing non-ascii characters in...

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I use imap-tools to access my emails.

    My problem is that I'm trying to access emails sent from someone whose email contains special characters like ø which I can't encode correctly because from_ accepts a string as input, so I am not getting anywhere.

    import imap_tools

    with imap_tools.MailBox('imap.gmx.net').login(email, password, 'INBOX') as mailbox:
    for msg in mailbox.fetch(imap_tools.AND(from_ = 'beskeder@mød.dk')):
    print('Found')


    I shortend my code. I am expecting my program to print Found when the email sent by beskeder@mød.dk is found in my mailbox. Other emails with no special characters are found.

    The error message:

    Traceback (most recent call last):
    File "/Users/user/Desktop/test.py", line 4, in <module>
    for msg in mailbox.fetch(imap_tools.AND(from_ = 'beskeder@mød.dk')):
    File "/Users/user/Library/Python/3.9/lib/python/site-packages/imap_tools/mailbox.py", line 130, in fetch
    nums = tuple((reversed if reverse else iter)(self.numbers(criteria, charset)))[limit_range]
    File "/Users/user/Library/Python/3.9/lib/python/site-packages/imap_tools/mailbox.py", line 67, in numbers
    encoded_criteria = criteria if type(criteria) is bytes else str(criteria).encode(charset)
    UnicodeEncodeError: 'ascii' codec can't encode character '\xf8' in position 17: ordinal not in range(128)


    I tried to add 'beskeder@hottemøder.dk'.encode('ascii', 'ignore') but it's not working either.

    Error:

    TypeError: "from_" expected str value, "<class 'int'>" received and when I convert it to str() nothing happens.

    Continue reading...

Compartilhe esta Página