How to send simple UTF-8 email in Python 3.x

Apparently, sending email in Python is painful. This is what I did to ease it.

Note: some arcane and/or ancient email clients will have trouble understanding these mails due to the completely ignored encoding rules. Please take that in account when using these solutions.

Summary: dead simple text

Just assemble the text by hand, and force send it via smtplib.

import smtplib
import os
import datetime

import conf

config file; place it as

host = ""
port =  587
tls =  true
username = ""
password = ""
sender = "XYZ <>"
to = "ABC <>"

def send_email( subject, content ):
    """ Send a simple, stupid, text, UTF-8 mail in Python """

    for ill in [ "\n", "\r" ]:
        subject = subject.replace(ill, ' ')

    headers = {
        'Content-Type': 'text/html; charset=utf-8',
        'Content-Disposition': 'inline',
        'Content-Transfer-Encoding': '8bit'
        'From': conf.sender,
        'Date':'%a, %d %b %Y  %H:%M:%S %Z'),
        'X-Mailer': 'python',
        'Subject': subject

    # create the message
    msg = ''
    for key, value in headers.items():
        msg += "%s: %s\n" % (key, value)

    # add contents
    msg += "\n%s\n"  % (content)

    s = smtplib.SMTP(, conf.port)

    if conf.tls:

    if conf.username and conf.password:
        s.login(conf.username, conf.password)

    print ("sending %s to %s" % (subject, headers['To']))
    s.sendmail(headers['From'], headers['To'], msg.encode("utf8"))

Encourage creation!

If this entry helped you, or you simply liked it, leave a tip.