How to send utf-8 e-mail?(如何发送UTF-8电子邮件?)
本文介绍了如何发送UTF-8电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何发送UTF8电子邮件?
import sys
import smtplib
import email
import re
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sendmail(firm, fromEmail, to, template, subject, date):
with open(template, encoding="utf-8") as template_file:
message = template_file.read()
message = re.sub(r"{{s*firms*}}", firm, message)
message = re.sub(r"{{s*dates*}}", date, message)
message = re.sub(r"{{s*froms*}}", fromEmail, message)
message = re.sub(r"{{s*tos*}}", to, message)
message = re.sub(r"{{s*subjects*}}", subject, message)
msg = MIMEMultipart("alternative")
msg.set_charset("utf-8")
msg["Subject"] = subject
msg["From"] = fromEmail
msg["To"] = to
#Read from template
html = message[message.find("html:") + len("html:"):message.find("text:")].strip()
text = message[message.find("text:") + len("text:"):].strip()
part1 = MIMEText(html, "html")
part2 = MIMEText(text, "plain")
msg.attach(part1)
msg.attach(part2)
try:
server = smtplib.SMTP("10.0.0.5")
server.sendmail(fromEmail, [to], msg.as_string())
return 0
except Exception as ex:
#log error
#return -1
#debug
raise ex
finally:
server.quit()
if __name__ == "__main__":
#debug
sys.argv.append("Moje")
sys.argv.append("newsletter@example.cz")
sys.argv.append("subscriber@example.com")
sys.argv.append("may2011.template")
sys.argv.append("This is subject")
sys.argv.append("This is date")
if len(sys.argv) != 7:
exit(-2)
firm = sys.argv[1]
fromEmail = sys.argv[2]
to = sys.argv[3]
template = sys.argv[4]
subject = sys.argv[5]
date = sys.argv[6]
exit(sendmail(firm, fromEmail, to, template, subject, date))
输出
Traceback (most recent call last):
File "C:Documents and SettingsAdministratorPlochaNewsletter-build-desktopsendmail.py", line 69, in <module>
exit(sendmail(firm, fromEmail, to, template, subject, date))
File "C:Documents and SettingsAdministratorPlochaNewsletter-build-desktopsendmail.py", line 45, in sendmail
raise ex
File "C:Documents and SettingsAdministratorPlochaNewsletter-build-desktopsendmail.py", line 39, in sendmail
server.sendmail(fromEmail, [to], msg.as_string())
File "C:Python32libsmtplib.py", line 716, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character 'u011b' in position 385: ordinal not in range(128)
推荐答案
您只需向MIMEText
调用添加'utf-8'
参数(默认情况下假定为'us-ascii'
)。
例如:
# -*- encoding: utf-8 -*-
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart("alternative")
msg["Subject"] = u'テストメール'
part1 = MIMEText(u'u3053u3093u306bu3061u306fu3001u4e16u754cuff01
',
"plain", "utf-8")
msg.attach(part1)
print msg.as_string().encode('ascii')
这篇关于如何发送UTF-8电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何发送UTF-8电子邮件?


基础教程推荐
猜你喜欢
- 合并具有多索引的两个数据帧 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01