django - how python sendmail with chinese characters? -


My environment is python3.3.4 django1.6.2, when I work on a project, and I need to send an email to users Via an email proxy service, but I went wrong with django send_mail, so I wrote a function with dragon stumpsp and email lib. The code is like:

  def email_user (reference): email = 'admin@***.cn' toEmail = ['** *' 'Qq.com'] msg = MIMEMultipart (' Optional ') msg.set_charset (' utf8 ') msg ​​[' subject '] =' 欢迎 注册 心 优雅 社区, '请 激活 您 的 账号' msg ['from'] = e-mail msg ['from'] = ','. Join (toEmail) html = "" 亲爱 的% (user name) s: 您好!您 在心 优雅 注册 注册 账号 时 使用 了 这个 邮箱, 现在 您 需 点击 下面 的 链接 激活 账号:% (protocol) s: //% (domain) s 如果 链接 无法 点击, 请将 它 完整 到 浏览 器 器 器器 栏 栏 进行 访问链接 有效期 为% (end_desk) s 天, 失效 后 需 重新 注册如果 您 并未 进行 过 此 操作, 么 么 可能 是 有人 误 用 了 您 的 邮箱, 请 忽略 此 邮件. "%" Section = MIMEText (html, 'html', _ charset = 'UTF-8') Msg.attach (part) username = 'postmaster @ *** org' password = '****' s = Smtplib.SMTP ('****. Com: 25') s.login (user name, password) s.sendmail (fromEmail, toEmail, Msg.as_string ()) s.quit ()  

When I test it in Python IDEL, it works well. But, when I call this function to send an email through my own Digengo project, the Unicode encode error makes me mad.

  in the position of 'ascii' codec 439-441 characters Can not encode: In non sequential order (128) Unicode error signal string that could not be encoded / decoded: 亲爱 的  

Can anyone help?

This is really sad to the initial people when I tried many solutions from SO or other websites, then this error It has come again and again, I think it should be a silly question, then I Nne changed to check whether there are spelling errors. Yes, I accidentally set parameters for MIMEText. So I changed the line of script to:

  MIMEText (html, 'html', 'utf8')  

now it is working .


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -