按照上面的代码执行之后,提示我
Traceback (most recent call last):
File "/usr/local b/python2.6/threading.py", line 525, in __bootstrap_inner
File "/home/houjw/monitor/DbMonitorThread.py", line 117, in run
File "/home/houjw/monitor/Alarm.py", line 15, in report
File "/home/houjw/monitor/myemail.py", line 23, in send_mail
File "/usr/local b/python2.6/email/mime/text.py", line 30, in __init__
File "/usr/local b/python2.6/email/message.py", line 224, in set_payload
File "/usr/local b/python2.6/email/message.py", line 264, in set_charset
File "/usr/local b/python2.6/email/encoders.py", line 73, in encode_7or8bit
LookupError: unknown encoding: ascii
File "/usr/local b/python2.6/threading.py", line 525, in __bootstrap_inner
File "/home/houjw/monitor/DbMonitorThread.py", line 117, in run
File "/home/houjw/monitor/Alarm.py", line 15, in report
File "/home/houjw/monitor/myemail.py", line 23, in send_mail
File "/usr/local b/python2.6/email/mime/text.py", line 30, in __init__
File "/usr/local b/python2.6/email/message.py", line 224, in set_payload
File "/usr/local b/python2.6/email/message.py", line 264, in set_charset
File "/usr/local b/python2.6/email/encoders.py", line 73, in encode_7or8bit
LookupError: unknown encoding: ascii
在同事的指导下在执行代码前添加
reload(sys)
sys.setdefaultencoding('utf8')
sys.setdefaultencoding('utf8')
就好了
email.mime.text.
MIMEText
class email.mime.text.MIMEText(_text[, _subtype[, _charset]])
Module: email.mime.text
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. If _text is unicode, it is encoded using the output_charset of _charset, otherwise it is used as-is
Module: email.mime.text
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. If _text is unicode, it is encoded using the output_charset of _charset, otherwise it is used as-is
推荐使用下面这种
msg = email.MIMEText.MIMEText(info, _subtype="html", _charset="utf-8")
http://suzhouclark.itpub.net/post/7184/484998