且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python 自动化运维 smtplib

更新时间:2022-09-27 13:31:08

  1.实现邮件的发送功能

  

  2.基础使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import smtplib     ##导入模块
import string
HOST = "smtp.163.com"    ##定义远程smtp主机
SUBJECT = " TEST"        ##定义发送主题
TO = "1287112485@qq.com"        ##定义接收主机
FROM = "18502942450@163.com"   ##定义发送主机
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("18502942450@163.com","ws128711")   ##校验远程主机
server.sendmail(FROM,[TO],BODY)      ##邮件发送功能[发送人,接收人,内容]
server.quit()

## 出现550错误时,打开邮箱客户端授权码

Python 自动化运维 smtplib



测试

Python 自动化运维 smtplib


   3.个性化邮件

MIME类:它是一个互联网标准,扩展了电子邮件标准,使其能够支持,非ASCII字符文本;非文本格式附件(二进制、声音、图像等),由多部分(multiple parts)组成的消息体,包含非ASCII字符的头信息。

实例:发送html文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import smtplib
from email.mime.text import MIMEText    ##导入MIMEText类
import string
HOST = "smtp.163.com"
SUBJECT = " TEST"
TO = "1287112485@qq.com"
FROM = "18502942450@163.com"
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&nbsp;</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">&nbsp;Added&nbsp;</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("18502942450@163.com","ws128711")
    server.sendmail(FROM,[TO],msg.as_string())
    server.quit()      ##断开连接
    print "ok"  
except Exception,e:
    print "error:" + str(e)

测试


Python 自动化运维 smtplib

本文转自  red777    51CTO博客,原文链接:http://blog.51cto.com/12314711/1971457