'Header-encode a string, optionally converting it to output_charset.
If convert is True, the string will be converted from the input
charset to the output charset automatically.  This is not useful for
multibyte character sets, which have line length issues (multibyte
characters must be split on a character, not a byte boundary); use the
high-level Header class to deal with these issues.  convert defaults
to False.
The type of encoding (base64 or quoted-printable) will be based on
self.header_encoding.'

def header_encode(self, s, convert=False):

    cset = self.get_output_charset()
    if convert:
        s = self.convert(s)
    if (self.header_encoding == BASE64):
        return email.base64mime.header_encode(s, cset)
    elif (self.header_encoding == QP):
        return email.quoprimime.header_encode(s, cset, maxlinelen=None)
    elif (self.header_encoding == SHORTEST):
        lenb64 = email.base64mime.base64_len(s)
        lenqp = email.quoprimime.header_quopri_len(s)
        if (lenb64 < lenqp):
            return email.base64mime.header_encode(s, cset)
        else:
            return email.quoprimime.header_encode(s, cset, maxlinelen=None)
    else:
        return s
