这篇文章主要介绍了python实现邮件自动发送的案例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联公司专注于企业营销型网站、网站重做改版、铁岭网站定制设计、自适应品牌网站建设、H5响应式网站、成都商城网站开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为铁岭等各大城市提供网站开发制作服务。case 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="""Python 邮件发送测试...
""" 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="""Python 邮件发送测试...
""" 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 = """Python 邮件发送添加图片测试...
图片演示:
""" 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 = """Python 邮件发送测试...
图片演示:
""" 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', '') 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在工作中基本够用了。
感谢你能够认真阅读完这篇文章,希望小编分享的“python实现邮件自动发送的案例”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!