【问题标题】:MIMEText UTF-8 encode problems when sending email发送电子邮件时的 MIMEText UTF-8 编码问题
【发布时间】:2011-12-31 14:14:33
【问题描述】:

这是我发送电子邮件的代码的一部分:

servidor = smtplib.SMTP()
servidor.connect(HOST, PORT)
servidor.login(user, usenha)
assunto = str(self.lineEdit.text())
para = str(globe_email)             
texto = self.textEdit.toPlainText()
textos = str(texto)
corpo = MIMEText(textos.encode('utf-8'), _charset='utf-8')
corpo['From'] = user
corpo['To'] = para
corpo['Subject'] = assunto
servidor.sendmail(user, [para], corpo.as_string())

除了主题部分之外,一切都很好。 当我尝试发送带有特殊字符(例如“ação”)的字符串时,它会引发此错误:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)

如何在 MIMEText 的主题中发送带有特殊字符的电子邮件?

【问题讨论】:

    标签: python email python-3.x unicode utf-8


    【解决方案1】:

    似乎在python3中,需要一个Header对象来将Subject编码为utf-8:

    # -*- coding: utf-8 -*-
    from email.mime.text import MIMEText
    from email.header import Header
    s = 'ação'
    m = MIMEText(s, 'plain', 'utf-8')
    m['Subject'] = Header(s, 'utf-8')
    print(repr(m.as_string()))
    

    输出:

    'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: base64\nSubject: =?utf-8?b?YcOnw6Nv?=\n\nYcOnw6Nv\n
    

    所以原来的脚本会变成:

    servidor = smtplib.SMTP()
    servidor.connect(HOST, PORT)
    servidor.login(user, usenha)
    assunto = str(self.lineEdit.text())
    para = str(globe_email)             
    texto = str(self.textEdit.toPlainText())
    corpo = MIMEText(texto, 'plain', 'utf-8')
    corpo['From'] = user
    corpo['To'] = para
    corpo['Subject'] = Header(assunto, 'utf-8')
    servidor.sendmail(user, [para], corpo.as_string())
    

    【讨论】:

    • 看来?它在文档中:docs.python.org/2/library/email.header.html
    • @AlMartins 是的,“似乎”。我刚刚使用 Python-3.2.6、Python-3.3.0 和 Python-3.8.6 对此进行了测试,无论是否使用 Header 对象,我都得到了完全相同的输出。这可能是由于compat32。据推测,OP 使用的是具有不同行为的早期版本的 Python3。所以它似乎当前的文档有些误导,因为Header 对象不是必需的。但是,为了向后兼容,似乎最好继续使用它。
    【解决方案2】:

    我已经改进了与服务器连接和登录的其他方式的答案,因为另一种方式我在验证应用程序时遇到了问题,人们可以看到所有应该使用的库

    from email.mime.text import MIMEText
    from email.header import Header
    import smtplib
    
    user='email1@teste.com'
    pwd='password'
    server = smtplib.SMTP('smtp.office365.com', 587) #it works with outlook
    server.ehlo()
    server.starttls()
    server.login(user, pwd)
    assunto = 'Teste'
    para = 'email2@teste.com'
    texto = 'Niterói é uma cidade incrível '
    corpo = MIMEText(texto, 'plain', 'utf-8')
    corpo['From'] = user
    corpo['To'] = para
    corpo['Subject'] = Header(assunto, 'utf-8')
    try:
        server.sendmail(user, [para], corpo.as_string())
        print('email was sent')
    except:
        print('error')
    server.quit()
    

    【讨论】:

    • 感谢您的回答,但您能否扩展一下这个答案?回顾[如何写出好的答案?](stackoverflow.com/help/how-to-answer)
    • 在使用 Flask 的 Python 3 上不成功。 html.encode('utf-8') 有效。
    猜你喜欢
    • 1970-01-01
    • 2011-06-21
    • 2017-08-27
    • 2011-08-20
    • 2011-11-26
    • 2015-02-10
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    相关资源
    最近更新 更多