pallets-eco/flask-mail

UTF-8 senders not fully supported

Closed this issue · 3 comments

Stacktrace:

  File "~/project/project.py", line 563, in test_mail
    conn.send(msg)
  File "~/.local/lib/python3.5/site-packages/flask_mail.py", line 188, in send
    self.host.sendmail(sanitize_address(envelope_from or message.sender),
  File "~/.local/lib/python3.5/site-packages/flask_mail.py", line 121, in sanitize_address
    return formataddr((nm, addr))
  File "/usr/lib/python3.5/email/utils.py", line 91, in formataddr
    address.encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\xf5' in position 1: ordinal not in range(128)

'\xf5' == õ

Flask app config:
MAIL_DEFAULT_SENDER = "fõõ@example.com"

Code that triggers the bug:

def test_mail():
    with mail.connect() as conn:
        msg = Message(recipients=["example@example.ee"],
                      body="examplebody",
                      subject="examplesubject")

        conn.send(msg)

Using:
Flask 0.12.2
Flask-Mail 0.9.1
Python 3.5.2

Relevant issue on Python's bug tracker: https://bugs.python.org/issue25955

Yeah, looks like the issue is with the sanitize_address method. What's interesting is that the code path with the comment # IDN seems to take a localpart turn it into a Header then back into a string. So it just round trips from fõõ back into fõõ. Pretty sure that isn't the desired behavior.

So I think its supposed to be encoded like '=?utf-8?b?ZsO1w7U=?=@example.com'

yeah just tested that string in my gmail account and it comes across as fõõ@example.com when it gets sent. Issue is in line 116. It should be localpart = Header(localpart, encoding).encode().

I can't reproduce this issue, sending is successful with the given sender.