您的位置:首页 > Web前端

python smtplib TypeError: expected string or buffe

2014-03-24 17:05 866 查看
问题描述:

写了一个发送纯文本邮件的脚本,执行的时候报错:

[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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐