Python 自動化運維 smtplib
阿新 • • 發佈:2017-10-12
python 自動化 運維
1.實現郵件的發送功能
2.基礎使用
import smtplib ##導入模塊 import string HOST = "smtp.163.com" ##定義遠程smtp主機 SUBJECT = " TEST" ##定義發送主題 TO = "[email protected]" ##定義接收主機 FROM = "[email protected]" ##定義發送主機 text = "python" ## ##定義郵件內容 BODY = string.join(( ##定義發送格式內容 "From: %s" %FROM, "To : %s" %TO, "Subject: %s " %SUBJECT, "", text ),"\r\n") server = smtplib.SMTP() ##實例化 server.connect(HOST,"25") ##連接遠程主機 server.starttls() ##啟用TLS(安全傳輸)模式 server.login("[email protected]","ws128711") ##校驗遠程主機 server.sendmail(FROM,[TO],BODY) ##郵件發送功能[發送人,接收人,內容] server.quit()
## 出現550錯誤時,打開郵箱客戶端授權碼
測試
3.個性化郵件
MIME類:它是一個互聯網標準,擴展了電子郵件標準,使其能夠支持,非ASCII字符文本;非文本格式附件(二進制、聲音、圖像等),由多部分(multiple parts)組成的消息體,包含非ASCII字符的頭信息。
實例:發送html文件
import smtplib from email.mime.text import MIMEText ##導入MIMEText類 import string HOST = "smtp.163.com" SUBJECT = " TEST" TO = "[email protected]" FROM = "[email protected]" msg = MIMEText(""" ##創建MIME對象,制定HTML內容,類型,字符編碼等 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title></title> <style type="text/css"> table.diff {font-family:Courier; border:medium;} .diff_header {background-color:#e0e0e0} td.diff_header {text-align:right} .diff_next {background-color:#c0c0c0} .diff_add {background-color:#aaffaa} .diff_chg {background-color:#ffff77} .diff_sub {background-color:#ffaaaa} </style> </head> <body> <table class="diff" id="difflib_chg_to0__top" cellspacing="0" cellpadding="0" rules="groups" > <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <tbody> <tr><td class="diff_next" id="difflib_chg_to0__0"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="from0_1">1</td><td nowrap="nowrap"></td><td class="diff_next"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="to0_1">1</td><td nowrap="nowrap"></td></tr> <tr><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="from0_2">2</td><td nowrap="nowrap">wwwwdwed</td><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="to0_2">2</td><td nowrap="nowrap">wwwwdwed<span class="diff_add">efwe</span></td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_3">3</td><td nowrap="nowrap"><span class="diff_sub">wwwdwqed </span></td><td class="diff_next"></td><td class="diff_header" id="to0_3">3</td><td nowrap="nowrap"><span class="diff_add">wwdwqedewf</span></td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_4">4</td><td nowrap="nowrap">wwefwe</td><td class="diff_next"></td><td class="diff_header" id="to0_4">4</td><td nowrap="nowrap">wwefwe</td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_5">5</td><td nowrap="nowrap">wwgwte</td><td class="diff_next"></td><td class="diff_header" id="to0_5">5</td><td nowrap="nowrap">wwgwte</td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_6">6</td><td nowrap="nowrap">wtgq</td><td class="diff_next"></td><td class="diff_header" id="to0_6">6</td><td nowrap="nowrap">wtgq</td></tr> </tbody> </table> <table class="diff" summary="Legends"> <tr> <th colspan="2"> Legends </th> </tr> <tr> <td> <table border="" summary="Colors"> <tr><th> Colors </th> </tr> <tr><td class="diff_add"> Added </td></tr> <tr><td class="diff_chg">Changed</td> </tr> <tr><td class="diff_sub">Deleted</td> </tr> </table></td> <td> <table border="" summary="Links"> <tr><th colspan="2"> Links </th> </tr> <tr><td>(f)irst change</td> </tr> <tr><td>(n)ext change</td> </tr> <tr><td>(t)op</td> </tr> </table></td> </tr> </table> </body> </html> ""","html",‘utf-8‘) msg["Subject"] = SUBJECT ##郵件主題 msg["From"] = FROM msg["to" ] = TO try: server = smtplib.SMTP() server.connect(HOST,"25") server.starttls() server.login("[email protected]","ws128711") server.sendmail(FROM,[TO],msg.as_string()) server.quit() ##斷開連接 print "ok" except Exception,e: print "error:" + str(e)
測試
Python 自動化運維 smtplib