Package email :: Module header :: Class Header
[hide private]
[frames] | no frames]

_ClassType Header

Instance Methods [hide private]
 
__init__(self, s=None, charset=None, maxlinelen=None, header_name=None, continuation_ws=' ', errors='strict')
Create a MIME-compliant header that can contain many character sets.
 
__str__(self)
A synonym for self.encode().
 
__unicode__(self)
Helper for the built-in unicode function.
 
__eq__(self, other)
 
__ne__(self, other)
 
append(self, s, charset=None, errors='strict')
Append a string to the MIME header.
 
_split(self, s, charset, maxlinelen, splitchars)
 
_split_ascii(self, s, charset, firstlen, splitchars)
 
_encode_chunks(self, newchunks, maxlinelen)
 
encode(self, splitchars=';, ')
Encode a message header into an RFC-compliant format.
Method Details [hide private]

__init__(self, s=None, charset=None, maxlinelen=None, header_name=None, continuation_ws=' ', errors='strict')
(Constructor)

 

Create a MIME-compliant header that can contain many character sets.

Optional s is the initial header value. If None, the initial header value is not set. You can later append to the header with .append() method calls. s may be a byte string or a Unicode string, but see the .append() documentation for semantics.

Optional charset serves two purposes: it has the same meaning as the charset argument to the .append() method. It also sets the default character set for all subsequent .append() calls that omit the charset argument. If charset is not provided in the constructor, the us-ascii charset is used both as s's initial charset and as the default for subsequent .append() calls.

The maximum line length can be specified explicit via maxlinelen. For splitting the first line to a shorter value (to account for the field header which isn't included in s, e.g. `Subject') pass in the name of the field in header_name. The default maxlinelen is 76.

continuation_ws must be RFC 2822 compliant folding whitespace (usually either a space or a hard tab) which will be prepended to continuation lines.

errors is passed through to the .append() call.

append(self, s, charset=None, errors='strict')

 

Append a string to the MIME header.

Optional charset, if given, should be a Charset instance or the name of a character set (which will be converted to a Charset instance). A value of None (the default) means that the charset given in the constructor is used.

s may be a byte string or a Unicode string. If it is a byte string (i.e. isinstance(s, str) is true), then charset is the encoding of that byte string, and a UnicodeError will be raised if the string cannot be decoded with that charset. If s is a Unicode string, then charset is a hint specifying the character set of the characters in the string. In this case, when producing an RFC 2822 compliant header using RFC 2047 rules, the Unicode string will be encoded using the following charsets in order: us-ascii, the charset hint, utf-8. The first character set not to provoke a UnicodeError is used.

Optional `errors' is passed as the third argument to any unicode() or ustr.encode() call.

encode(self, splitchars=';, ')

 

Encode a message header into an RFC-compliant format.

There are many issues involved in converting a given string for use in an email header. Only certain character sets are readable in most email clients, and as header strings can only contain a subset of 7-bit ASCII, care must be taken to properly convert and encode (with Base64 or quoted-printable) header strings. In addition, there is a 75-character length limit on any given encoded header field, so line-wrapping must be performed, even with double-byte character sets.

This method will do its best to convert the string to the correct character set used in email, and encode and line wrap it safely with the appropriate scheme for that character set.

If the given charset is not known or an error occurs during conversion, this function will return the header untouched.

Optional splitchars is a string containing characters to split long ASCII lines on, in rough support of RFC 2822's `highest level syntactic breaks'. This doesn't affect RFC 2047 encoded lines.