python smtplib TypeError: expected string or buffe
2014-03-24 17:05
866 查看
问题描述:
写了一个发送纯文本邮件的脚本,执行的时候报错:
sender_plain.py代码:
知道原因么?python bug !
读完这篇之后,我也想说“
”
这已经是python2.7了!!
https://mail.python.org/pipermail/python-bugs-list/2010-March/093097.html
写了一个发送纯文本邮件的脚本,执行的时候报错:
[root@server1 test]# python2.7 sender_plain.py Traceback (most recent call last): File "sender_plain.py", line 36, in <module> send_mail(send_from, reply_to, send_to, subject, text) File "sender_plain.py", line 25, in send_mail smtp.sendmail(send_from, reply_to, send_to, msg.as_string()) File "/usr/local/lib/python2.7/smtplib.py", line 736, in sendmail (code, resp) = self.data(msg) File "/usr/local/lib/python2.7/smtplib.py", line 499, in data q = quotedata(msg) File "/usr/local/lib/python2.7/smtplib.py", line 166, in quotedata re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data)) File "/usr/local/lib/python2.7/re.py", line 151, in sub return _compile(pattern, flags).sub(repl, string, count) TypeError: expected string or buffer
sender_plain.py代码:
#coding=utf-8 import smtplib, os from email.message import Message from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.Utils import COMMASPACE, formatdate def send_mail(send_from, send_to, subject, text, server="localhost", port=4467): assert type(send_to)==list # 创建MIME,并添加信息头 msg = MIMEMultipart() msg['To'] = COMMASPACE.join(send_to) msg['From']=send_from msg['Subject'] = subject msg['Date'] = formatdate(localtime = 1) # 创建MIMEText,并添加到msg body = MIMEText(text,_subtype = "plain",_charset="utf-8") msg.attach(body) print msg.as_string() smtp = smtplib.SMTP(server,port) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close() if __name__=="__main__": send_from = "test@163.com" send_to = ["test01@qq.com","test02@qq.com","test03@qq.com"] reply_to = "test04@qq.com" subject = u"发送纯文本邮件,无附件内容" text = u"发送纯文本邮件,无附件内容" send_mail(send_from, send_to, subject, text)
知道原因么?python bug !
读完这篇之后,我也想说“
i can't understand how this simple mistake does not be noticed
”
这已经是python2.7了!!
https://mail.python.org/pipermail/python-bugs-list/2010-March/093097.html
相关文章推荐
- python写入文本报错TypeError: expected a string or other character buffer object
- Python出现: TypeError: expected string or buffer
- 【tensorFlow】tf.reshape()报错信息 - TypeError: Expected binary or unicode string, got -1
- Python TypeError: must be string or buffer, not dict
- 解决:error: Cannot fetch repo (TypeError: expected string or buffer)
- django 接受post请求json.dumps()的时候会引发TypeError: 'expected string or buffer'错误
- TypeError: expected string or bytes-like object
- Python TypeError: coercing to Unicode: need string or buffer, float found
- a=re.findall('b',c)报错提示:TypeError:expected string or buffer
- TypeError: expected string or buffer
- uwsgi部署web,error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
- python出现,coercing to unicode need string or buffer nonetype found python 错误的解决
- 解决python中TypeError: not enough arguments for format string
- python错误:TypeError: string indices must be integers
- spark 报错ypeError: expected string or Unicode object, NoneType found
- Qt报错:myudp.h:4: error: expected identifier or '(' before string constant
- python TypeError: Expected int32, got list containing Tensors of type '_Message' instead.
- Python出现TypeError: file() argument 1 must be encoded string without NULL bytes, not str问题解决
- Python的坑(2) -- TypeError: 'builtin_function_or_method' object is not subscriptable
- python 点滴记录4:TypeError: range() integer end argument expected, got str.