“latin1” is unknown and so defaults to Base64 MIME. In fact, this is why Base64 was
used for the “latin1” Unicode type earlier in this section—an encoding choice that is
irrelevant to any recipient that understands the “latin1” synonym, including Python
itself. Unfortunately, that means that we also need to pass in a different string type if
we use a synonym the package doesn’t understand today:
>>> m = MIMEText('abc', _charset='latin-1') # str for 'latin-1'
>>> print(m)
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
abc
>>> m = MIMEText('abc', _charset='latin1')
Traceback (most recent call last):
...lines omitted...
File "C:\Python31\lib\email\base64mime.py", line 94, in body_encode
enc = b2a_base64(s[i:i + max_unencoded]).decode("ascii")
TypeError: must be bytes or buffer, not str
>>> m = MIMEText(b'abc', _charset='latin1') # bytes for 'latin1'!
>>> print(m)
Content-Type: text/plain; charset="latin1"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
YWJj
There are ways to add aliases and new encoding types in the email package, but they’re
not supported out of the box. Programs that care about being robust would have to
cross-check the user’s spelling, which may be valid for Python itself, against that ex-
pected by email. This also holds true if your data is not ASCII in general—you’ll have
to first decode to text in order to use the expected “latin-1” name because its quoted-
printable MIME encoding expects str, even though bytes are required if “latin1”
triggers the default Base64 MIME:
>>> m = MIMEText(b'A\xe4B', _charset='latin1')
>>> print(m)
Content-Type: text/plain; charset="latin1"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
QeRC
>>> m = MIMEText(b'A\xe4B', _charset='latin-1')
Traceback (most recent call last):
...lines omitted...
File "C:\Python31\lib\email\quoprimime.py", line 176, in body_encode
if line.endswith(CRLF):
TypeError: expected an object with the buffer interface
>>> m = MIMEText(b'A\xe4B'.decode('latin1'), _charset='latin-1')
>>> print(m)
email: Parsing and Composing Mail Content | 943