This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: email.MIMEText overide BASE64 for utf8 charset
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: r.david.murray Nosy List: Blame-me.Jaillie, r.david.murray
Priority: normal Keywords:

Created on 2011-07-13 14:37 by Blame-me.Jaillie, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg140259 - (view) Author: Blame-me Jaillie (Blame-me.Jaillie) Date: 2011-07-13 14:37
Apologies if this is in the wrong place.

Simple enough issue. This line of code from email.mime:

MIMEText(textonly, 'plain', _charset='UTF-8')
Where 'textonly' is just a plain text email message to be displayed on a multipart message in a client that does not support HTML email.

This always results in: Content-Transfer-Encoding: BASE64
rather than allowing selection of the encoder (7 or 8 bit MIME/quoted printable). The option to set this with _encoders was removed.

This presents a couple of issues. First of all, BASE64 is not optimal for text - it adds (granted small) amounts of overhead and CPU usage. Second, commercial and O/S anti-spam scanners have rules that penalise messages solely BASE64 encoded.

As this is part of the mime email package, a simple flag to set the Content-Transfer-Encoding by hand would be help anyone trying to produce sensible email applications with Python.

Whilst my version of Python is old - I believe this issue remains in later versions.
msg140286 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-07-13 16:41
This should do what you want:

  from email import charset
  charset.add_charset('utf-8', charset.SHORTEST, charset.QP)

This will override the default handling of utf-8 (which is BASE64, as you note).

If this doesn't solve your problem please reopen the issue.

It is a valid feature request (for 3.3) to have a way to make this '8bit'.  I think I'll open an issue for that.
msg140288 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-07-13 16:50
Opened issue 12553 for the 8bit feature request.
msg140291 - (view) Author: Blame-me Jaillie (Blame-me.Jaillie) Date: 2011-07-13 17:33
Very much obliged to you - as pointed out by R David Murray:

  from email import charset
  charset.add_charset('utf-8', charset.SHORTEST, charset.QP)

This works exactly as expected - but to expand for anyone else who happens across this, this will result in quoted printable transfer encoding. Removing the final 'charset.QP' results in (for me) the desired 7bit. 

Again, sincere thanks to R David Murray for pointing me in the correct way, and making my first experience with a Python 'issue' painless and flame free. Really appreciated.
msg140294 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-07-13 18:02
Good point.  In 2.x that will work, and will give you the 8bit CTE at need, as long as you pass encoded text to MIMEText (as opposed to unicode...which it doesn't really handle correctly if I recall right).
History
Date User Action Args
2022-04-11 14:57:19adminsetgithub: 56761
2011-07-13 18:02:47r.david.murraysetmessages: + msg140294
2011-07-13 17:33:09Blame-me.Jailliesetmessages: + msg140291
2011-07-13 16:50:50r.david.murraysetmessages: + msg140288
2011-07-13 16:41:10r.david.murraysetstatus: open -> closed

assignee: r.david.murray
components: + Library (Lib), - None
title: email.MIMEText overide BASE64 on TEXT/HTML -> email.MIMEText overide BASE64 for utf8 charset
nosy: + r.david.murray

messages: + msg140286
resolution: not a bug
stage: resolved
2011-07-13 14:37:34Blame-me.Jailliecreate