python 邮件发送
环境:python2.7
1 #coding:utf-8 2 from __future__ import unicode_literals 3 __author__ = 'crista' 4 5 import smtplib 6 from email.mime.text import MIMEText 7 from email.mime.application import MIMEApplication 8 from email.mime.multipart import MIMEMultipart 9 from email.header import Header 10 import ConfigParser 11 12 #message :正文内容,数据类型为string 13 #files:所添加附件绝对路径加名字.文件类型,数据类型为list 14 15 def send_file(msg,files): 16 ##添加附件部分 17 for f in files : 18 attr = MIMEApplication(open(f,'rb').read()) 19 ##输入中文,会产生获取的文件名字乱码问题 20 filename=f.split('\\') 21 num=len(filename)-1 22 attr.add_header('Content-Disposition', 'attachment', filename=filename[num]) 23 msg.attach(attr) 24 25 ##群发邮件加附件函数 26 def send_email(title,message,files,reciever): 27 ##我用的是读取配置文件变量模块(ConfigParser),可直接赋值更方便 28 cf =ConfigParser.ConfigParser() 29 cf.read("config.ini") 30 sender=cf.get('smtp',"sender") ##此填入发件人 31 mail_host=cf.get('smtp','mail_host') ##邮箱主机 如:SMTP.163.com 32 mail_port=cf.get('smtp','mail_port') ##端口:一般为25 33 send_pass=cf.get('smtp','send_pass') ##邮箱授权码 34 35 36 for i in reciever: 37 print i 38 msg = MIMEMultipart() 39 msg['From']=sender 40 msg['To'] =i 41 msg['Subject']=Header(title,'utf-8') 42 msg.attach(MIMEText(message, 'plain', 'utf-8')) 43 try: 44 if files != []: 45 send_file(msg,files) ##调用上面send_file() 46 except: 47 pass 48 try: 49 server = smtplib.SMTP() 50 server.connect(mail_host,mail_port) 51 server.login(sender, send_pass) 52 server.sendmail(sender,i, msg.as_string()) 53 print "邮件发送成功!!" 54 server.quit() 55 except smtplib.SMTPException: 56 print "Error:无法发送邮件" 57 58 59 60 if __name__ == "__main__": 61 ##可添加多个收件邮箱和附件(但附件必须要带文件类型如 .jpg) 62 send_email("test","server failed",[],["邮箱账号1","邮箱账号2"])
可能出现的报错:
smtplib 模块
1、smtplib.SMTPAuthenticationError: (550, b'User has no permission')
2、smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')
这两个错误是你的密码用的不是授权码导致
3、554 DT:SPM 163 smtp5
这属于网易邮箱的一个退回检测机制导致,貌似跟发送比较频繁有关,被系统判定为病毒或垃圾邮件
稍微完善版:
import smtplib from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.header import Header import ConfigParser #message :正文内容,数据类型为string #files:所添加附件绝对路径加名字.文件类型,数据类型为list class email: def __init__(self): cf =ConfigParser.ConfigParser() cf.read("config.ini") self.sender=cf.get('smtp',"sender") self.mail_host=cf.get('smtp','mail_host') self.mail_port=cf.get('smtp','mail_port') self.send_pass=cf.get('smtp','send_pass') def send_file(self,files): ##添加附件部分 for f in files : attr = MIMEApplication(open(f,'rb').read()) ##输入中文,会产生获取的文件名字乱码问题 filename=f.split('\\') num=len(filename)-1 attr.add_header('Content-Disposition', 'attachment', filename=filename[num]) msg.attach(attr) ##群发邮件加附件函数 def send_email(self,title,message,files,reciever): for i in reciever: self.msg = MIMEMultipart() self.msg['From']=self.sender self.msg['To'] =i self.msg['Subject']=Header(title,'utf-8') self.msg.attach(MIMEText(message, 'plain', 'utf-8')) try: if files != []: self.send_file(files) except: pass try: server = smtplib.SMTP() server.connect(self.mail_host,self.mail_port) server.login(self.sender, self.send_pass) server.sendmail(self.sender,i, self.msg.as_string()) print "邮件发送成功!!" server.quit() except smtplib.SMTPException: print "Error:无法发送邮件" if __name__ == "__main__": Email=email() Email.send_email("test","server failed",[],["收件邮箱1"])