python实现邮件自动发送
人气:0case 1:纯文本和HTML文件发送
# -*- coding: UTF-8 -*- import smtplib import traceback from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def sendmail(subject,msg,toaddrs,fromaddr,smtpaddr,password): ''' @subject:邮件主题 @msg:邮件内容 @toaddrs:收信人的邮箱地址 @fromaddr:发信人的邮箱地址 @smtpaddr:smtp服务地址,可以在邮箱看,比如163邮箱为smtp.163.com @password:发信人的邮箱密码 ''' mail_msg = MIMEMultipart() if not isinstance(subject,unicode): #过滤或者不过滤貌似没啥影响 subject = unicode(subject, 'utf-8') mail_msg['Subject'] = subject mail_msg['From'] =fromaddr mail_msg['To'] = ','.join(toaddrs) mail_msg.attach(MIMEText(msg, 'plain', 'utf-8')) #f发送文本文件 #mail_msg.attach(MIMEText(msg, 'html', 'utf-8')) #发送html格式邮件 try: s = smtplib.SMTP() s.connect(smtpaddr) #连接smtp服务器 s.login(fromaddr,password) #登录邮箱 s.sendmail(fromaddr, toaddrs, mail_msg.as_string()) #发送邮件 s.quit() print "邮件发送成功!" except Exception,e: print "Error: unable to send email" print traceback.format_exc() if __name__ == '__main__': fromaddr = "h******m@126.com" smtpaddr = "smtp.126.com" toaddrs = ["83****789@qq.com","h******m@126.com"] subject = "Hello,我是邮件主题" password = "不能告诉你" #msg = "Hello,我是邮件内容 !!!" msg=""" <p>Python 邮件发送测试...</p> <p><a href="http://www.runoob.com" >这是一个链接</a></p> """ sendmail(subject,msg,toaddrs,fromaddr,smtpaddr,password)
case 2.添加附件:发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送。
# -*- coding: UTF-8 -*- # 如何添加附件 import os import smtplib import traceback from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart os.chdir("C:\\Users\\h****m\\Desktop\\Auto_email") def sendmail(subject,msg,toaddrs,fromaddr,smtpaddr,password): ''' @subject:邮件主题 @msg:邮件内容 @toaddrs:收信人的邮箱地址 @fromaddr:发信人的邮箱地址 @smtpaddr:smtp服务地址,可以在邮箱看,比如163邮箱为smtp.163.com @password:发信人的邮箱密码 ''' mail_msg = MIMEMultipart() if not isinstance(subject,unicode): #过滤或者不过滤貌似没啥影响 subject = unicode(subject, 'utf-8') mail_msg['Subject'] = subject mail_msg['From'] =fromaddr mail_msg['To'] = ','.join(toaddrs) #mail_msg.attach(MIMEText(msg, 'plain', 'utf-8')) #f发送文本文件 mail_msg.attach(MIMEText(msg, 'html', 'utf-8')) #发送html格式邮件 #构造附件1 att1=MIMEText(open("test_file1.txt",'rb').read(),'base64','utf-8') #注意:直接读取中文文件名会报错 att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = 'attachment; filename="test_file1.txt"' mail_msg.attach(att1) #构造附件2:添加中文附件名 att2=MIMEText(open(u'测试文件2.docx','rb').read(),'base64','utf-8') att2["Content-Type"] = 'application/octet-stream' att2["Content-Disposition"] = 'attachment; filename="test_file2.docx"' mail_msg.attach(att2) try: s = smtplib.SMTP() s.connect(smtpaddr) #连接smtp服务器 s.login(fromaddr,password) #登录邮箱 s.sendmail(fromaddr, toaddrs, mail_msg.as_string()) #发送邮件 s.quit() print "邮件发送成功!" except Exception,e: print "Error: unable to send email" print traceback.format_exc() if __name__ == '__main__': fromaddr = "h****m@126.com" smtpaddr = "smtp.126.com" toaddrs = ["83****789@qq.com","h****m@126.com"] subject = "Hello,我是邮件主题" password = "不能告诉你" #msg = "Hello,我是邮件内容 !!!" msg=""" <p>Python 邮件发送测试...</p> <p><a href=http://www.runoob.com >这是一个链接</a></p> """ sendmail(subject,msg,toaddrs,fromaddr,smtpaddr,password)
case 3: 发送图片(容易被判别为垃圾邮件,导致:SMTPDataError: (554, 'DT:SPM 126……错误)
# -*- coding: utf-8 -*- fromaddr = "h****m@126.com" smtpaddr = "smtp.126.com" toaddrs = ["83****789@qq.com","h****m@126.com"] subject = "Hello,我是邮件主题" password = "不能告诉你" #test3 msg = """ <p>Python 邮件发送添加图片测试...</p> <p><a href="http://www.runoob.com" >菜鸟教程链接</a></p> <p>图片演示:</p> <p><img src="cid:image1"></p> """ import os import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage os.chdir("C:\\Users\\h****m\\Desktop\\Auto_email") msgRoot = MIMEMultipart('related') if not isinstance(subject,unicode): #过滤或者不过滤貌似没啥影响 subject = unicode(subject, 'utf-8') msgRoot['Subject'] = subject msgRoot['From'] =fromaddr msgRoot['To'] = ','.join(toaddrs) msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msg = """ <p>Python 邮件发送测试...</p> <p>图片演示:</p> <p><img src="cid:image1"></p> """ msgAlternative.attach(MIMEText(msg, 'html', 'utf-8')) fp = open('test.PNG', 'rb') msgImage = MIMEImage(fp.read()) fp.close() # 定义图片 ID,在 HTML 文本中引用 msgImage.add_header('Content-ID', '<image1>') msgRoot.attach(msgImage) s = smtplib.SMTP() s.connect(smtpaddr) #连接smtp服务器 s.login(fromaddr,password) #登录邮箱 s.sendmail(fromaddr, toaddrs, msgRoot.as_string()) #发送邮件 s.quit() print "邮件发送成功!" #邮件发送成功!
对于case3,发送很不稳定,时而发送得出,时而报 SMTPDataError: 554错误,等有空了,看有没有方法弄个稳定的版本。case1,case2在工作中基本够用了。
您可能感兴趣的文章:
加载全部内容