flask-mail icon indicating copy to clipboard operation
flask-mail copied to clipboard

UTF-8 senders not fully supported

Open Avamander opened this issue 7 years ago • 2 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=["[email protected]"],
                      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

Avamander avatar Dec 30 '17 13:12 Avamander

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 '[email protected]'

androiddrew avatar Jan 29 '18 01:01 androiddrew

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().

androiddrew avatar Jan 29 '18 03:01 androiddrew

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

davidism avatar May 23 '24 18:05 davidism