Python email - 8bit MIME support
Question: I'm writing a simple MUA application, and I have a troubles with generating
message.

I want to my program automatically detect whether the SMTP server supports
`8bit MIME`, and if yes, then it'll generate a message, where the part with
plain text will be encoded on 8bits. In MIME header it should looks like that:

`Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-
Encoding: 8bit`

The major problem is that, python3.4 smtplib hasn't `8-bit encoder`, just only
`base64`, and `quoted printable`

In my case it looks that:

    
    
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    msg = MIMEMultipart()
    msg['From'] = 'someone@example.com'
    msg['To'] = 'someone@example.com'
    msg['Subject'] = 'subject'
    
    text = MIMEText("text here".encode('utf-8'), _charset='utf-8')
    
    msg.attach(text)
    
    # then sending...
    

The `text.as_string()` call returns

    
    
    'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: base64\n\ndGV4dCBoZXJl\n'
    

This message is quite good, but I'd want to have `8-bit` enconding, not
`base64`.

The question is am I really convicted to use `base64` enconding?

In `email.encoders` are only `encode_base64` and `encode_quopri` functions


Answer: Default body encoding for utf-8 is BASE64 which can be replaced locally:

    
    
        from email import charset
        ch = charset.Charset('utf-8')
        ch.body_encoding = '8bit'
        text = MIMEText("")
        text.set_charset(ch)
        text.set_payload("text here")
        text.replace_header('Content-Transfer-Encoding', '8bit')
        msg.attach(text)
    

or globally:

    
    
        from email import charset
        charset.add_charset('utf-8', charset.SHORTEST, '8bit')
    
        text = MIMEText("text here".encode('utf-8'), _charset='utf-8')
        text.replace_header('Content-Transfer-Encoding', '8bit')
    
        msg.attach(text)
    

