def email_user(context):
fromEmail = 'admin@***.cn'
toEmail = ['***@qq.com']
msg = MIMEMultipart('alternative')
msg.set_charset('utf8')
msg['Subject'] = '欢迎注册心优雅社区,请激活您的账号'
msg['From'] = fromEmail
msg['To'] = ','.join(toEmail)
html = """
亲爱的%(username)s:
您好!
您在心优雅社区注册账号时使用了这个邮箱,现在您需要点击下面的链接激活该账号:
%(protocol)s://%(domain)s
如果链接无法点击,请将它完整复制到浏览器的地址栏进行访问.
链接有效期为%(expiration_days)s天,失效后需重新注册.
如果您并未进行过此操作,那么可能是有人误用了您的邮箱,请忽略此邮件."""%context
part = MIMEText(html,'html',_charset='UTF-8')
msg.attach(part)
username = 'postmaster@***org'
password = '****'
s = smtplib.SMTP('****.com:25')
s.login(username,password)
s.sendmail(fromEmail,toEmail,msg.as_string())
s.quit()When I test it in the python IDLE,it work well. But,when I called this function to send email in my django project,or run it in "manage.py shell",UnicodeEncodeError let me crazy.
'ascii' codec can't encode characters in position 439-441: ordinal not in range(128)
Unicode error hint
The string that could not be encoded/decoded was: 亲爱的