.sendmail函数尝试转换为ASCII,我必须转换吗? [英] .sendmail function trying to convert to ASCII, do I have to convert?

查看:168
本文介绍了.sendmail函数尝试转换为ASCII,我必须转换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试使用python发送电子邮件,但只要将其转换为ASCII,我就无法解决,是否有办法解决?还是需要寻找其他功能?

So I'm trying to send an email using python but I can't as long as it converts it to ASCII, is there a way around this or do I need to find another function?

File "/usr/lib/python3.6/smtplib.py", line 855, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 1562: ordinal not in range(128)

我可以解决这个问题还是可以转换?

Can I get around this or do I have convert? and how would i convert?

推荐答案

传统上,SMTP要求您提交的消息采用ASCII格式。问题是在过程的早期:尝试传递的消息在创建时应该已经转换为正确的MIME消息。

Traditionally, SMTP requires the message you submit to be in ASCII. The problem is earlier in the process: The message you are trying to pass in should already have been converted into a proper MIME message when you created it.

通常,任何二进制数据应转换为具有 Content-Transfer-Encoding:base64 ,并且任何非ASCII文本都应具有 Content-Transfer-Encoding:quoted-printable 。然后,您可以安全地使用非ASCII字节,并且传输编码将透明地将有效负载转换为ASCII进行传输,而收件人的电子邮件软件则负责按预期显示它。

Generally, any binary data should be converted to have a Content-Transfer-Encoding: base64 and any non-ASCII text should have Content-Transfer-Encoding: quoted-printable. Then you can safely use non-ASCII bytes and the transfer encoding takes care of transparently converting the payload to ASCII for transport, and the recipient's email software takes care of displaying it as you intended.

Python的电子邮件库已经知道如何处理这些事情。也许您正在尝试手动构造一条消息,而没有实际检查规格说明?但是使用标准库显然要容易得多,并且可以使您免于学习的麻烦。

Python's email library already knows how to take care of these things. Perhaps you are trying to construct a message manually without actually checking what the specs say? But using the standard library is obviously easier and saves you from a fair bit of learning curve.

有关具体细节,请参见例如如何通过MIMEMultipart发送电子邮件正文部分

For concrete details, see e.g. how to send a email body part through MIMEMultipart

现在有扩展SMTP以处理UTF-8的规定,但错误消息表明您的Sendmail尚未完成任务。 (或者也许可以添加一个选项来配置,但这远远超出了这个问题以及Stack Overflow的范围。)

There are now provisions for extending SMTP to handle UTF-8 everywhere, but the error message suggests that your Sendmail is not yet up to the task. (Or perhaps there is an option you can add to its configuration, but that's far outside the scope of this question, and of Stack Overflow.)

这篇关于.sendmail函数尝试转换为ASCII,我必须转换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆