'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.'

def encode(self, splitchars=';,    '):

    newchunks = []
    maxlinelen = self._firstlinelen
    lastlen = 0
    for (s, charset) in self._chunks:
        targetlen = ((maxlinelen - lastlen) - 1)
        if (targetlen < charset.encoded_header_len('')):
            targetlen = maxlinelen
        newchunks += self._split(s, charset, targetlen, splitchars)
        (lastchunk, lastcharset) = newchunks[(-1)]
        lastlen = lastcharset.encoded_header_len(lastchunk)
    value = self._encode_chunks(newchunks, maxlinelen)
    if _embeded_header.search(value):
        raise HeaderParseError('header    value    appears    to    contain    an    embedded    header:    {!r}'.format(value))
    return value
