import smtplibmsg = mimemultipart()

#构造附件1att1 = mimetext(open(‘/home/a2bgeek/develop/python/’, ‘rb’).read(), ‘base64’, ‘gb2312’)att1[“content-type”] = ‘application/octet-stream’att1[“content-disposition”] = ‘attachment; filename=”hello.txt”‘#这里的filename可以任意写,写什么名字,邮件中显示什么名字msg.attach(att1)

#构造附件2#att2 = mimetext(open(‘/home/a2bgeek/develop/python/’, ‘rb’).read(), ‘base64’, ‘gb2312’)#att2[“content-type”] = ‘application/octet-stream’#att2[“content-disposition”] = ‘attachment; filename=”123.txt”‘#msg.attach(att2)

#加邮件头strto = [‘’, ‘’, ‘’]msg[‘to’]=’,’.join(strto)msg[‘from’] = ‘’msg[‘subject’] = ‘邮件主题’#发送邮件try: server = smtplib.smtp() server.connect(‘’) server.login(‘’,’yourpasswd’) server.sendmail(msg[‘from’], strto ,msg.as_string()) server.quit() print ‘发送成功’except exception, e: print str(e)


the problem is that smtp.sendmail and email.mimetext need two different things.

email.mimetext sets up the “to:” header for the body of the e-mail. it is only used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (note that it does not actually have to have anything to do with the people who actually receive the message.)

smtp.sendmail, on the other hand, sets up the “envelope” of the message for the smtp protocol. it needs a python list of strings, each of which has a single address.

so, what you need to do is combine the two replies you received. set msg‘to’ to a single string, but pass the raw list to sendmail.


Posted in 未分类