大家好,我在unicode邮箱中遇到了这个问题,当我试图用西班牙语发送诸如"Añadir"或其他系统崩溃的单词时,我try 了这个链接上的内容:Python 3 smtplib send with unicode characters,但不起作用.

以下是我的错误代码:

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"编解码器无法对位置7中的字符"\xf1"进行编码:序号不在范围内(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字符,您仍然会遇到问题.你不能在那里使用标题.

Python-3.x相关问答推荐

使用Polars阅读按日期键分区的最新S3镶木地板文件

网站抓取:当我使用Chrome DevTools中的网络选项卡时,找不到正确的URL来提供我想要的数据

使用递归将int转换为字符串

动态范围内来自另外两列的列求和

在循环中使用Print&S结束参数时出现奇怪的问题

Strawberry FastAPI:如何调用正确的函数?

将两列的乘积连续添加到一列的累积和中

汉明距离:涉及按位运算的逻辑步骤不清楚

Django中自动设置/更新字段

从一列字符串中提取子字符串并将它们放入列表中

python 分代垃圾收集:get_count 没有报告正确的对象创建数?

无法使用 Python 和 Selenium 检索 href 属性

通过 requests 库调用 API 获取访问令牌

将字节数组转换为类似字节的对象?

multiprocessing.Queue 中的 ctx 参数

cv2 python 没有 imread 成员

创建集合的 Python 性能比较 - set() 与 {} 文字

为什么 Python 不能识别我的 utf-8 编码源文件?

Python 3.4 多处理队列比 Pipe 快,出乎意料

如何使用 python http.server 运行 CGI hello world