Подтвердить что ты не робот

Как установить кодировку в электронной почте с помощью smtplib в Python 2.7?

Я пишу простого smtp-отправителя с аутентификацией. Здесь мой код

    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

Он работает идеально, пока я не пытаюсь отправить символы не-ascii (русская кириллица). Как мне определить кодировку в сообщении, чтобы она отображалась надлежащим образом? Спасибо заранее!

UPD. Я изменил свой код:

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))

Итак, в первый раз я просматриваю text_subtype = 'text', а затем в заголовке помещаю строку msg ['Content-Type'] = "text/html; charset = utf-8". Правильно ли это?

ОБНОВЛЕНИЕ Наконец, я решил проблему с сообщениями Вы должны написать smth как msg = MIMEText (content.encode('utf-8'), 'plain', 'UTF-8')

4b9b3361

Ответ 1

from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def contains_non_ascii_characters(str):
    return not all(ord(c) < 128 for c in str)   

def add_header(message, header_name, header_value):
    if contains_non_ascii_characters(header_value):
        h = Header(header_value, 'utf-8')
        message[header_name] = h
    else:
        message[header_name] = header_value    
    return message

............
msg = MIMEMultipart('alternative')
msg = add_header(msg, 'Subject', subject)

if contains_non_ascii_characters(html):
    html_text = MIMEText(html.encode('utf-8'), 'html','utf-8')
else:
    html_text = MIMEText(html, 'html')    

if(contains_non_ascii_characters(plain)):
    plain_text = MIMEText(plain.encode('utf-8'),'plain','utf-8') 
else:
    plain_text = MIMEText(plain,'plain')

msg.attach(plain_text)
msg.attach(html_text)

Это должно дать вам правильную кодировку для текста и заголовков независимо от того, содержит ли ваш текст символы, отличные от ASCII, или нет. Это также означает, что вы не будете автоматически использовать кодировку base64 без необходимости.

Ответ 3

Возможно, вам придется использовать SMTP-заголовок для достижения разных кодировок, попробуйте добавить это -

msg['Content-Type'] = "text/html; charset=us-ascii"

(Измените кодировку в соответствии с вашими потребностями)