Comment définir un jeu de caractères dans le courrier électronique en utilisant smtplib dans Python 2.7?

Je suis en train d'écrire un simple expéditeur-smtp avec authentification. Voici mon code

    SMTPserver, sender, destination = 'smtp.googlemail.com', '[email protected]', ['[email protected]']
    USERNAME, PASSWORD = "user", "password"

    # typical values for text_subtype are plain, html, xml
    text_subtype = 'plain'


    content="""
    Hello, world!
    """

    subject="Message Subject"

    from smtplib import SMTP_SSL as SMTP       # this invokes the secure SMTP protocol (port 465, uses SSL)
    # from smtplib import SMTP                  # use this for standard SMTP protocol   (port 25, no encryption)
    from email.MIMEText import MIMEText

    try:
        msg = MIMEText(content, text_subtype)
        msg['Subject']=       subject
        msg['From']   = sender # some SMTP servers will do this automatically, not all

        conn = SMTP(SMTPserver)
        conn.set_debuglevel(False)
        conn.login(USERNAME, PASSWORD)
        try:
            conn.sendmail(sender, destination, msg.as_string())
        finally:
            conn.close()

    except Exception, exc:
        sys.exit( "mail failed; %s" % str(exc) ) # give a error message

Il fonctionne parfaitement, jusqu'à ce que je essayer de transmettre des symboles ascii (cyrillique russe). Comment dois-je définir un jeu de caractères dans un message à la faire apparaître dans une manière appropriée? Merci à l'avance!

UPD. J'ai changé mon code:

text_subtype = 'text'
content="<p>Текст письма</p>"
msg = MIMEText(content, text_subtype)
msg['From']=sender # some SMTP servers will do this automatically, not all
msg['MIME-Version']="1.0"
msg['Subject']="=?UTF-8?Q?Тема письма?="
msg['Content-Type'] = "text/html; charset=utf-8"
msg['Content-Transfer-Encoding'] = "quoted-printable"

conn.sendmail(sender, destination, str(msg))

Donc, la première fois que je spectify text_subtype = "texte", puis dans l'en-tête, je place un msg['Content-Type'] = "text/html; charset=utf-8" de la chaîne. Est-il correct?

Mise à JOUR Enfin, j'ai résolu mon problème message d'
Vous devriez écrire qch comme msg = MIMEText(le contenu.encode('utf-8'), 'plaine', 'UTF-8')

source d'informationauteur f1nn