在 Python 3.1 中使用 unicode 字符发送邮件的 smtplib 问题 [英] Issue with smtplib sending mail with unicode characters in Python 3.1
问题描述
您好,我在使用 unicode 电子邮件时遇到了这个问题,当我尝试用西班牙语发送诸如Añadir"或其他内容时,系统崩溃了,我已经尝试过此链接上的内容:Python 3 smtplib 使用 unicode 字符发送 并且不起作用.
Hello i' ve this problem with unicode emails, when i try to send words in spanish like: "Añadir" or others the system collapse, i've try what says on this link: Python 3 smtplib send with unicode characters and doesn't work.
这是我的错误代码:
server.sendmail(frm, to, msg.as_string())
g.flatten(self, unixfrom=unixfrom)
self._write(msg)
self._write_headers(msg)
header_name=h)
self.append(s, charset, errors)
input_bytes = s.encode(input_charset, errors)
UnicodeEncodeError: 'ascii' codec can't encode character '\xf1' in position 7: ordinal not in range(128)
UnicodeEncodeError: 'ascii' codec can't encode character '\xf1' in position 7: ordinal not in range(128)
这是服务器上的代码:
msg = MIMEMultipart('alternative')
frm = "sales@bmsuite.com"
msg['FROM'] = frm
to = "info@bmsuite.com"
msg['To'] = to
msg['Subject'] = "Favor añadir esta empresa a la lista"
_attach = MIMEText("""Nombre:Prueba; Dirección:Calle A #12.""".encode('utf-8'), _charset='utf-8')
msg.attach(_attach)
server.sendmail(frm, to, msg.as_string())
server.quit()
提前致谢.
推荐答案
您可以改为使用:
msg = MIMEText(message, _charset="UTF-8")
msg['Subject'] = Header(subject, "utf-8")
但无论哪种方式,如果您的 frm = "xxxx@xxxxxx.com"
或 to = "xxxx@xxxxxx.com"
包含 unicode 字符,您仍然会遇到问题.你不能在那里使用 Header.
But either way you still have issues if your frm = "xxxx@xxxxxx.com"
or to = "xxxx@xxxxxx.com"
constains unicode characters. You can't use Header there.
这篇关于在 Python 3.1 中使用 unicode 字符发送邮件的 smtplib 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!