温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python怎么实现批量邮件推送且可支持html邮件格式

发布时间:2022-02-22 16:38:10 来源:亿速云 阅读:131 作者:iii 栏目:开发技术

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

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
# 第三方 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 = 'Python SMTP 邮件测试'
msgRoot['Subject'] = Header(subject, 'utf-8')
 
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
 
 
# mail_msg = """
# <p>Python 邮件发送测试...</p>
# <p><a href="https://www.亿速云.com">组学大讲堂</a></p>
# <p>图片演示:</p>
# <p><img src="cid:image1"></p>
# 
# 
# 
# 
# 
# 
# """

#这里支持html 格式 输入页面
mail_msg = """
<html>
<head>
    
   <title>Tutsplus Email Newsletter</title>
   <style type="text/css">
    a {color: #d80a3e;}
  body, #header h2, #header h3, p {margin: 0; padding: 0;}
  #main {border: 1px solid #cfcece;}
  img {display: block;}
  #top-message p, #bottom p {color: #3f4042; font-size: 12px; font-family: Arial, Helvetica, sans-serif; }
  #header h2 {color: #ffffff !important; font-family: "Lucida Grande", sans-serif; font-size: 24px; margin-bottom: 0!important; padding-bottom: 0; }
  #header p {color: #ffffff !important; font-family: "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode", sans-serif; font-size: 12px;  }
  h6 {margin: 0 0 0.8em 0;}
    h6 {font-size: 18px; color: #444444 !important; font-family: Arial, Helvetica, sans-serif; }
  p {font-size: 12px; color: #444444 !important; font-family: "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode", sans-serif; line-height: 1.5;}
   </style>
</head>
<body>
<table width="100%" cellpadding="0" cellspacing="0" bgcolor="e4e4e4"><tr><td>
<table id="top-message" cellpadding="20" cellspacing="0" width="600" align="center">
    <tr>
      <td align="center">
        <p><a href="#">View in Browser</a></p>
      </td>
    </tr>
  </table>
 
<table id="main" width="600" align="center" cellpadding="0" cellspacing="15" bgcolor="ffffff">
    <tr>
      <td>
        <table id="header" cellpadding="10" cellspacing="0" align="center" bgcolor="8fb3e9">
          <tr>
            <td width="570" align="center"  bgcolor="#d80a3e"><h2>Evanto Limited</h2></td>
          </tr>
          <tr>
            <td width="570" align="right" bgcolor="#d80a3e"><p>November 2017</p></td>
          </tr>
        </table>
      </td>
    </tr>
 
    <tr>
      <td>
        <table id="content-3" cellpadding="0" cellspacing="0" align="center">
          <tr>
              <td width="250" valign="top" bgcolor="d0d0d0" >
              <img src="https://cache.yisu.com/upload/information/20220118/470/125771.jpg" width="250" height="150"  />
            </td>
              <td width="15"></td>
            <td width="250" valign="top" bgcolor="d0d0d0" >
                <img src="https://cache.yisu.com/upload/information/20220118/470/125774.jpg" width ="250" height="150" />
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table id="content-4" cellpadding="0" cellspacing="0" align="center">
          <tr>
            <td width="200" valign="top">
              <h6>How to Get Up and Running With Vue</h6>
              <p>In the introductory post for this series we spoke a little about how web designers can benefit by using Vue. In this tutorial we’ll learn how to get Vue up..</p>
            </td>
            <td width="15"></td>
            <td width="200" valign="top">
              <h6>Introducing Haiku: Design and Create Motion</h6>
              <p>With motion on the rise amongst web developers so too are the tools that help to streamline its creation. Haiku is a stand-alone..</p>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
  <table id="bottom" cellpadding="20" cellspacing="0" width="600" align="center">
    <tr>
      <td align="center">
        <p>Design better experiences for web & mobile</p>
        <p><a href="#">Unsubscribe</a> | <a href="#">Tweet</a> | <a href="#">View in Browser</a></p>
      </td>
    </tr>
  </table><!-- top message -->
</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 "邮件发送成功"
except smtplib.SMTPException:
    print "Error: 无法发送邮件"

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI