Mar*_*lík 35 python email smtp 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*firm\s*}}", firm, message)
message = re.sub(r"{{\s*date\s*}}", date, message)
message = re.sub(r"{{\s*from\s*}}", fromEmail, message)
message = re.sub(r"{{\s*to\s*}}", to, message)
message = re.sub(r"{{\s*subject\s*}}", 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))
Run Code Online (Sandbox Code Playgroud)
产量
> Traceback (most recent call last):
> File "C:\Documents and Settings\Administrator\Plocha\Newsletter-build-desktop\sendmail.py",
> line 69, in <module>
> exit(sendmail(firm, fromEmail, to, template, subject, date))
>
> File "C:\Documents and Settings\Administrator\Plocha\Newsletter-build-desktop\sendmail.py",
> line 45, in sendmail
> raise ex
>
> File "C:\Documents and Settings\Administrator\Plocha\Newsletter-build-desktop\sendmail.py",
> line 39, in sendmail
> server.sendmail(fromEmail, [to], msg.as_string())
>
> File "C:\Python32\lib\smtplib.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)
Run Code Online (Sandbox Code Playgroud)
abb*_*bot 70
您应该只'utf-8'为您的MIMEText调用添加参数('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'\u3053\u3093\u306b\u3061\u306f\u3001\u4e16\u754c\uff01\n',
"plain", "utf-8")
msg.attach(part1)
print msg.as_string().encode('ascii')
Run Code Online (Sandbox Code Playgroud)
Martin Drl\xc3\xadk 提出的问题已经有 7 年零 8 个月了……现在,感谢 Python 的开发者,编码问题在 Python 3 版本中得到了解决。
\n因此,不再需要指定必须使用 utf-8 编码:
\n#!/usr/bin/python2\n# -*- encoding: utf-8 -*-\n...\n part2 = MIMEText(text, "plain", "utf-8")\nRun Code Online (Sandbox Code Playgroud)\n我们将简单地写:
\n#!/usr/bin/python3\n...\n part2 = MIMEText(text, "plain")\nRun Code Online (Sandbox Code Playgroud)\n最终结果:Martin Drl\xc3\xadk\ 的脚本运行得非常好!
\n但是,最好使用 email.parser 模块,如电子邮件中建议的:示例中所建议的。
\n| 归档时间: |
|
| 查看次数: |
38309 次 |
| 最近记录: |