Python编码电子邮件MIMEMultipart

时间:2014-01-16 13:26:54

标签: python html email

所以,这是我的代码,从python.org获取,但是当我发送包含特殊字符的电子邮件时,例如“é”或“não”,它会显示为“é”或“n”在outlook.com上“,”但其他电子邮件也可以。

有什么办法可以解决这个问题吗?我尝试设置.py编码,html元enconding但没有工作。

    copia = str(copia)
    msg = MIMEMultipart('alternative')
    msg['Subject'] = assunto
    msg['From'] = de
    msg['To'] = para
    msg['Cc'] = copia           
    # Create the body of the message (a plain-text and an HTML version).
    text = email_texto
    html = email_html

    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')

    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
    msg.attach(part1)
    msg.attach(part2)


    server = smtplib.SMTP('smtp.gmail.com:587')
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(de, self.senha)
    server.sendmail(de, (para, copia), msg.as_string())
    server.quit()

1 个答案:

答案 0 :(得分:0)

您应该使用Unicode编码(UTF-8)。这是一个例子:

http://www.askthepony.com/blog/2011/06/how-to-send-a-proper-unicode-encoded-email-using-python-2-7/