[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
'"=?UTF-8?Q?Walmart?=" <[email protected]>, "=?UTF-8?Q?Walmart?=" <newslet
[email protected]>'

>>> pairs = getaddresses([rawtoheader])
>>> pairs
[('=?UTF-8?Q?Walmart?=', '[email protected]'), ('=?UTF-8?Q?Walmart?=', 'ne
[email protected]')]

>>> addrs = []
>>> for name, addr in pairs:
... abytes, aenc = decode_header(name)[0] # email+MIME
... name = abytes.decode(aenc) # Unicode
... addrs.append(formataddr((name, addr))) # one or more addrs
...
>>> ', '.join(addrs)
'Walmart <[email protected]>, Walmart <[email protected]>'

These tools are generally forgiving for unencoded content and return them intact. To
be robust, though, the last portion of code here should also allow for multiple parts
returned by decode_header (for encoded substrings), None encoding values for parts (for
unencoded substrings), and str substring values instead of bytes (for fully unencoded
names).


Decoding this way applies both MIME and Unicode decoding steps to fetched mails.
Creating properly encoded headers for inclusion in new mails composed and sent is
similarly straightforward:


>>> from email.header import make_header
>>> hdr = make_header([(b'A\xc4B\xe4C', 'latin-1')])
>>> print(hdr)
AÄBäC
>>> print(hdr.encode())
=?iso-8859-1?q?A=C4B=E4C?=
>>> decode_header(hdr.encode())
[(b'A\xc4B\xe4C', 'iso-8859-1')]

This can be applied to entire headers such as Subject, as well as the name component
of each email address in an address-related header line such as From and To (use
getaddresses to split into individual addresses first if needed). The header object pro-
vides an alternative interface; both techniques handle additional details, such as line
lengths, for which we’ll defer to Python manuals:


>>> from email.header import Header
>>> h = Header(b'A\xe4B\xc4X', charset='latin-1')
>>> h.encode()
'=?iso-8859-1?q?A=E4B=C4X?='
>>>
>>> h = Header('spam', charset='ascii') # same as Header('spam')
>>> h.encode()
'spam'

The mailtools package ahead and its PyMailGUI client of Chapter 14 will use these
interfaces to automatically decode message headers in fetched mails per their content
for display, and to encode headers sent that are not in ASCII format. That latter also


email: Parsing and Composing Mail Content | 937
Free download pdf