Skip to content

Non-ascii SUBJECT and FROM turns into " =?utf-8?b?" even when charset is set to iso-8859-2 #126

@zoltan-fedor

Description

@zoltan-fedor

Hi,
So this is an interesting one.
I am trying to send email with a non-ascii subject line (Hungarian). The problem is that at the first non-ascii character the " =?utf-8?" string gets added - which is not a problem, but when the email client - both GMail and Yahoo Mail displays it that additional space is left there. So basically at random places there are spaces added to the subject line.

Settings the encoding specifically to iso-8859-2 doesn't help, as it seems the UTF8 encodeded version always takes precendence.

Sending the email like this:

    app = current_app._get_current_object()
    msg = Message(subject='Sikeres regisztracio es belepo a SF Kiallitasra (belépő)',
                              sender=('SF Biztonságtechnikai Kiállítás & Konferencia', 'kiallitas@sf.com'),
                              recipients=[('Fedoráéő Zoltan' , 'e@yahoo.com')],
                              charset='iso-8859-2', bcc=[bcc])
    msg.body = render_template((template + '.txt'), **kwargs).encode('iso-8859-2')
    msg.html = render_template((template + '.html'), **kwargs).encode('iso-8859-2')
    print(msg.charset)
    thr = Thread(target=send_async_email, args=[app, msg])
    thr.start()

This is what flask-mail generates:
Content-Type: multipart/mixed; boundary="===============2657123796784931499=="\r\nMIME-Version: 1.0\r\nSubject: Sikeres regisztracio es belepo az SF Kiallitasra (b\r\n =?utf-8?b?ZWzDqXDFkSk=?=\r\nFrom: SF =?utf-8?q?Biztons=C3=A1gtechnikai?=\r\n =?utf-8?b?S2nDoWxsw610w6Fz?= & Konferen cia <kiallitas@s.com>\r\nTo: =?iso-8859-2?q?Fedor=E1=E9=F5_Zoltan?= <e@yahoo.com>\r\nDate: Fri, 18 Mar 2016 22:50:40 +0100\r\nMessage-ID: <145833784033.2011.13923204934281192694@sf.com>\r\n\r\n--===============2657123796784931499==\r\nContent-Type: multipart/alternative;\r\n boundary="===============5571071455436956408=="\r\nMIME-Version: 1.0\r\n\r\n--===============5571071455436956408==\r\nContent-Type: text/plain; charset="iso-8859-2"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\nTisztelt Fedor=E1=E9=F5 Zoltan etc

As you can see, the funny thing is that the TO field correctly encoded with iso-8859-2, but the Subject and From is still encoded with utf-8. Those encodings are fine (if the client knows utf-8), but they added an additional " =?utf-8?", so after the client encoding it it will have an additional space/loses a space.

So in GMail and Yahoo Mail this will show like:

From: SF BiztonságtechnikaiKiállítás & Konferen cia (kiallitas@sf.com)
To: Fedoráéő Zoltan (e@yahoo.com)
Subject: Sikeres regisztracio es belepo a SF Kiallitasra (b elépő)

  • observe the additional space in the subject "(b elépő)" and the additional and lost spaces in the "FROM" field.

If you look at the header above, then you can see that the reason why the TO is displaying correctly and the FROM and SUBJECT doesn't that the "\r\n =?utf-8?b?" is inserted mid-text and that additional space is sometimes adds a text to display, sometimes taken away.

Interesting thing is that the flask_mail.py file's sanitize_subject() method correctly returns the iso-8859-r encoded version of the subject, but what makes into the email header is that utf8 encoded MIME version.

Any idea how to actually use Flask-Mail to send international characters in the FROM and SUBJECT field without additional / lost spaces?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions