这篇文章主要讲解了“python怎么实现批量邮件推送且可支持html邮件格式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python怎么实现批量邮件推送且可支持html邮件格式”吧!

利用python批量发送邮件,推广课程等等,可借助以下代码完成:

#!/usr/bin/python#-*-coding:UTF-8-*-importsmtplibfromemail.mime.textimportMIMETextfromemail.headerimportHeaderfromemail.mime.multipartimportMIMEMultipartfromemail.mime.imageimportMIMEImage#第三方SMTP服务mail_host="smtp.exmail.qq.com"#设置服务器mail_user="XXXX@biomics.com.cn"#用户名mail_pass="***********"#密码sender='XXXX@biomics.com.cn'receivers=['XXX3@126.com',"xxx@biomics.com.cn"]#接收邮件,可设置为你的QQ邮箱或者其他邮箱#创建一个带附件的实例msgRoot=MIMEMultipart('related')msgRoot['From']=Header("组学大讲堂",'utf-8')msgRoot['To']=Header("组学大讲堂学员",'utf-8')subject='PythonSMTP邮件测试'msgRoot['Subject']=Header(subject,'utf-8')msgAlternative=MIMEMultipart('alternative')msgRoot.attach(msgAlternative)#mail_msg="""#<p>Python邮件发送测试...</p>#<p><ahref="https://www.亿速云.com">组学大讲堂</a></p>#<p>图片演示:</p>#<p><imgsrc="cid:image1"></p>#######"""#这里支持html格式输入页面mail_msg="""<html><head><title>TutsplusEmailNewsletter</title><styletype="text/css">a{color:#d80a3e;}body,#headerh2,#headerh3,p{margin:0;padding:0;}#main{border:1pxsolid#cfcece;}img{display:block;}#top-messagep,#bottomp{color:#3f4042;font-size:12px;font-family:Arial,Helvetica,sans-serif;}#headerh2{color:#ffffff!important;font-family:"LucidaGrande",sans-serif;font-size:24px;margin-bottom:0!important;padding-bottom:0;}#headerp{color:#ffffff!important;font-family:"LucidaGrande","LucidaSans","LucidaSansUnicode",sans-serif;font-size:12px;}h6{margin:000.8em0;}h6{font-size:18px;color:#444444!important;font-family:Arial,Helvetica,sans-serif;}p{font-size:12px;color:#444444!important;font-family:"LucidaGrande","LucidaSans","LucidaSansUnicode",sans-serif;line-height:1.5;}</style></head><body><tablewidth="100%"cellpadding="0"cellspacing="0"bgcolor="e4e4e4"><tr><td><tableid="top-message"cellpadding="20"cellspacing="0"width="600"align="center"><tr><tdalign="center"><p><ahref="#">ViewinBrowser</a></p></td></tr></table><tableid="main"width="600"align="center"cellpadding="0"cellspacing="15"bgcolor="ffffff"><tr><td><tableid="header"cellpadding="10"cellspacing="0"align="center"bgcolor="8fb3e9"><tr><tdwidth="570"align="center"bgcolor="#d80a3e"><h2>EvantoLimited</h2></td></tr><tr><tdwidth="570"align="right"bgcolor="#d80a3e"><p>November2017</p></td></tr></table></td></tr><tr><td><tableid="content-3"cellpadding="0"cellspacing="0"align="center"><tr><tdwidth="250"valign="top"bgcolor="d0d0d0"><imgsrc="https://cache.yisu.com/upload/information/20220118/470/125771.jpg"width="250"height="150"/></td><tdwidth="15"></td><tdwidth="250"valign="top"bgcolor="d0d0d0"><imgsrc="https://cache.yisu.com/upload/information/20220118/470/125774.jpg"width="250"height="150"/></td></tr></table></td></tr><tr><td><tableid="content-4"cellpadding="0"cellspacing="0"align="center"><tr><tdwidth="200"valign="top"><h6>HowtoGetUpandRunningWithVue</h6><p>IntheintroductorypostforthisserieswespokealittleabouthowwebdesignerscanbenefitbyusingVue.Inthistutorialwe’lllearnhowtogetVueup..</p></td><tdwidth="15"></td><tdwidth="200"valign="top"><h6>IntroducingHaiku:DesignandCreateMotion</h6><p>Withmotionontheriseamongstwebdeveloperssotooarethetoolsthathelptostreamlineitscreation.Haikuisastand-alone..</p></td></tr></table></td></tr></table><tableid="bottom"cellpadding="20"cellspacing="0"width="600"align="center"><tr><tdalign="center"><p>Designbetterexperiencesforweb&mobile</p><p><ahref="#">Unsubscribe</a>|<ahref="#">Tweet</a>|<ahref="#">ViewinBrowser</a></p></td></tr></table><!--topmessage--></td></tr></table><!--wrapper--></body></html>"""msgAlternative.attach(MIMEText(mail_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)#邮件正文内容msgRoot.attach(MIMEText('这是组学大讲堂发送的邮件邮件发送测试……','plain','utf-8'))#构造附件1,传送当前目录下的test.txt文件att1=MIMEText(open('test.txt','rb').read(),'base64','utf-8')att1["Content-Type"]='application/octet-stream'#这里的filename可以任意写,写什么名字,邮件中显示什么名字att1["Content-Disposition"]='attachment;filename="test.txt"'msgRoot.attach(att1)##构造附件2,传送当前目录下的runoob.txt文件#att2=MIMEText(open('runoob.txt','rb').read(),'base64','utf-8')#att2["Content-Type"]='application/octet-stream'#att2["Content-Disposition"]='attachment;filename="runoob.txt"'#msgRoot.attach(att2)try:#smtpObj=smtplib.SMTP()#smtpObj.connect(mail_host,465)#25为SMTP端口号smtpObj=smtplib.SMTP_SSL(mail_host,465)#smtpObj.set_debuglevel(1)smtpObj.login(mail_user,mail_pass)smtpObj.sendmail(sender,receivers,msgRoot.as_string())print"邮件发送成功"exceptsmtplib.SMTPException:print"Error:无法发送邮件"

感谢各位的阅读,以上就是“python怎么实现批量邮件推送且可支持html邮件格式”的内容了,经过本文的学习后,相信大家对python怎么实现批量邮件推送且可支持html邮件格式这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!