如何通过Python 2.7发送电子邮件时解决编码问题?

时间:2017-07-24 15:20:19

标签: python django python-2.7 email encoding

我的Python 2.7中有以下代码,作为我的网站中联系表单的后端:

import sys
import smtplib
from django.http import HttpResponse


form = request.POST



from_addr = form['name']+' <'+form['email']+'>'
to_addrs = ['myemail@address']
msg = ("From: %s\r\nTo: %s\r\nSubject: Email sent from my website form\r\n\r\n"
   % (from_addr, ", ".join(to_addrs)))


msg = msg + form['message']


try:
    s = smtplib.SMTP('smtp.address')
    s.starttls()
    s.login('myemail@address', 'mypassword')
    s.sendmail(from_addr, to_addrs, msg.encode('utf-8'))
    s.quit()

except smtplib.SMTPException:
    return HttpResponse(status=404)

return HttpResponse(status=200)

此代码有效,但是...如果我发送“¿Enquéañosestamos?”这样的消息它以“En qu ao estamos?”的形式到达我的收件箱(它删除了一些字符)。我该如何解决这个问题?

0 个答案:

没有答案