python 邮件发送
程序员文章站
2022-03-30 20:22:03
环境:python2.7 可能出现的报错: smtplib 模块1、smtplib.SMTPAuthenticationError: (550, b'User has no permission')2、smtplib.SMTPAuthenticationError: (535, b'Error: a ......
环境: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"])
推荐阅读
-
Python 批量合并多个txt文件的实例讲解
-
Python使用Windows API创建窗口示例【基于win32gui模块】
-
python字符串大小写转换
-
python day 15: IO多路复用,socketserver源码培析,
-
电子邮件处理控件Aspose.Email for .NET 10月新版更新上线!
-
python day 14: 作业:开发一个能够多用户上传文件的FTP脚本
-
python 练习题:计算的BMI指数,并根据BMI指数条件选择
-
python分包写入文件,写入固定字节内容,当包达到指定大小时继续写入新文件
-
python学习笔记--深拷贝与浅拷贝的区别
-
【索引】Python导航