python smtplib TypeError: expected string or buffe

问题描述:

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

[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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值