一、兩個模塊
Python使用SMTP發送郵件的兩個模塊:smtplib模塊、email模塊。
- smtplib:負責發送郵件
- email:負責構建郵件
二、SMTP端口
1)未加密端口,smtplib.SMTP接口,端口:25
2)使用SSL加密,smtplib.SMTP_SSL接口,端口:465
3)使用TLS加密,端口:587
三、四大步驟
1、構造郵件內容
1
2
3
4
5
|
# 純文本 msg = MIMEText(content) # 附件 msg = MIMEMultipart() |
2、連接郵件服務器
1
|
s = smtplib.SMTP( "smtp.qq.com" , 25 ) |
3、登陸郵件服務器
1
|
s.login(msg_from, passwd) |
msg_from:指發送者的郵箱
passwd:指發送者的密碼,這個密碼不是你的QQ登陸密碼,而是你在QQ郵箱設置開啟SMTP之后的一個授權碼
4、發送郵件
1
|
s.sendmail(msg_from, msg_to, msg.as_string()) |
msg_from:發送方
msg_to:收件方
msg.as_string():要發送的消息
四、常用場景
1、純文本郵件
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
|
import smtplib from email.mime.text import MIMEText from email.header import Header # 發送者 # 這里的密碼不是QQ郵箱的密碼,而是在設置里開啟SMTP服務器后的授權碼 passwd = "xxxxx" # 接受者 # 郵件文本 content = 'Python 郵件發送測試...' # 郵件主題 subject = "test" # 生成一個MIMEText對象(還有一些其它參數) msg = MIMEText(content) # 放入郵件主題 msg[ 'Subject' ] = Header(subject, 'utf-8' ) # 放入發件人 msg[ 'From' ] = msg_from try : # 連接郵件服務器 s = smtplib.SMTP( "smtp.qq.com" , 25 ) # 登錄到郵箱 s.login(msg_from, passwd) # 發送郵件:發送方,收件方,要發送的消息 s.sendmail(msg_from, msg_to, msg.as_string()) print ( '成功' ) except s.SMTPException as e: print (e) finally : s.quit() |
2、發送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
|
import smtplib from email.mime.text import MIMEText from email.header import Header # 發送者 # 這里的密碼不是QQ郵箱的密碼,而是在設置里開啟SMTP服務器后的授權碼 passwd = "xxxx" # 接受者 # 郵件文本 content = """ <p>Python 郵件發送測試...</p> <p><a href="http://www.baidu.com" rel="external nofollow" >這是一個鏈接</a></p> """ # 郵件主題 subject = "test" # 生成一個MIMEText對象( msg = MIMEText(content, 'html' , 'utf-8' ) # 放入郵件主題 msg[ 'Subject' ] = Header(subject, 'utf-8' ) # 放入發件人 msg[ 'From' ] = msg_from try : # 連接郵件服務器 s = smtplib.SMTP( "smtp.qq.com" , 25 ) # 登錄到郵箱 s.login(msg_from, passwd) # 發送郵件:發送方,收件方,要發送的消息 s.sendmail(msg_from, msg_to, msg.as_string()) print ( '成功' ) except s.SMTPException as e: print (e) finally : s.quit() |
3、發送附件
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
|
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header # 發送者 # 這里的密碼不是QQ郵箱的密碼,而是在設置里開啟SMTP服務器后的授權碼 passwd = "xxxx" # 接受者 # 郵件主題 subject = "test" # 生成一個MIMEMultipart對象( msg = message = MIMEMultipart() # 郵件文本 message.attach(MIMEText( '這是菜鳥教程Python 郵件發送測試……' , 'plain' , 'utf-8' )) # 放入郵件主題 msg[ 'Subject' ] = Header(subject, 'utf-8' ) # 放入發件人 msg[ 'From' ] = msg_from # 添加附件 att1 = MIMEText( open ( './wordcloud_singer.py' , 'rb' ).read(), 'base64' , 'utf-8' ) att1[ "Content-Type" ] = 'application/octet-stream' att1[ "Content-Disposition" ] = 'attachment; filename="test.txt"' msg.attach(att1) try : # 連接郵件服務器 s = smtplib.SMTP( "smtp.qq.com" , 25 ) # 登錄到郵箱 s.login(msg_from, passwd) # 發送郵件:發送方,收件方,要發送的消息 s.sendmail(msg_from, msg_to, msg.as_string()) print ( '成功' ) except s.SMTPException as e: print (e) finally : s.quit() |
以上就是Python 發送SMTP郵件的簡單教程的詳細內容,更多關于Python 發送郵件的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/lemon-le/p/14858267.html