您好,登錄后才能下訂單哦!
這篇文章主要講解了“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郵件格式這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。