Python 3 smtplib send with unicode characters

二次信任 提交于 2019-11-27 11:22:38

问题


I'm having a problem emailing unicode characters using smtplib in Python 3. This fails in 3.1.1, but works in 2.5.4:

  import smtplib
  from email.mime.text import MIMEText

  sender = to = 'ABC@DEF.com'
  server = 'smtp.DEF.com'
  msg = MIMEText('€10')
  msg['Subject'] = 'Hello'
  msg['From'] = sender
  msg['To'] = to
  s = smtplib.SMTP(server)
  s.sendmail(sender, [to], msg.as_string())
  s.quit()

I tried an example from the docs, which also failed. http://docs.python.org/3.1/library/email-examples.html, the Send the contents of a directory as a MIME message example

Any suggestions?


回答1:


The key is in the docs:

class email.mime.text.MIMEText(_text, _subtype='plain', _charset='us-ascii')

A subclass of MIMENonMultipart, the MIMEText class is used to create MIME objects of major type text. _text is the string for the payload. _subtype is the minor type and defaults to plain. _charset is the character set of the text and is passed as a parameter to the MIMENonMultipart constructor; it defaults to us-ascii. No guessing or encoding is performed on the text data.

So what you need is clearly, not msg = MIMEText('€10'), but rather:

msg = MIMEText('€10'.encode('utf-8'), _charset='utf-8')

While not all that clearly documented, sendmail needs a byte-string, not a Unicode one (that's what the SMTP protocol specifies); look to what msg.as_string() looks like for each of the two ways of building it -- given the "no guessing or encoding", your way still has that euro character in there (and no way for sendmail to turn it into a bytestring), mine doesn't (and utf-8 is clearly specified throughout).




回答2:


_charset parameter of MIMEText defaults to us-ascii according to the docs. Since is not from us-ascii set it isn't working.

example in the docs that you've tried clearly states:

For this example, assume that the text file contains only ASCII characters.

You could use .get_charset method on your message to investigate the charset, there is incidentally .set_charset as well.




回答3:


Gus Mueller had a similar issue: http://bugs.python.org/issue4403



来源:https://stackoverflow.com/questions/1429147/python-3-smtplib-send-with-unicode-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!