需求背景
假設我們想設計一個定時任務,比如每天定時的用python來測試服務是否在正常運行,但是又不希望每天登錄到系統后臺去查看服務狀態。這里我們就可以采取python的smtp模塊進行任務結果廣播,申請一個公共郵箱,每次python執行完定時的測試任務后,調用smtp的接口將測試結果廣播給需要接收的人的郵箱中。這就使得,我們可以在移動端就能按照我們的意愿實時監測系統的狀態。
qq郵箱的smtp服務配置流程
1.瀏覽器登錄進入qq郵箱
2.進入設置-賬戶
3.找到pop3-smtp服務的位置,點擊開啟
4.進行一些驗證程序
5.開啟成功后,系統會給出一串長度為16的隨機口令,用來替代密碼進行第三方登錄
6.配置smtp服務器地址和端口號
使用授權碼登錄smtp并發送郵件
這里我們直接展示成果代碼,其中一些隱私信息做了處理:
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
|
# smtp_test.py #!/usr/bin/python # -*- coding: utf-8 -*- import smtplib from email.mime.text import mimetext from email.header import header # 第三方 smtp 服務 mail_host = "smtp.qq.com" #設置服務器 mail_pass = "passpasspasspass" #口令 message = mimetext( 'python smtp 郵件發送測試...' , 'plain' , 'utf-8' ) message[ 'from' ] = header( "smtp email" , 'utf-8' ) message[ 'to' ] = header( "test message" , 'utf-8' ) subject = 'python smtp 郵件測試' message[ 'subject' ] = header(subject, 'utf-8' ) try : smtpobj = smtplib.smtp() smtpobj.connect(mail_host, 25 ) # 25 為 smtp 端口號 smtpobj.login(mail_user,mail_pass) smtpobj.sendmail(sender, receivers, message.as_string()) print ( "郵件發送成功" ) except smtplib.smtpexception: import traceback traceback.print_exc() print ( "無法發送郵件" ) |
這里的服務器配置的smtp的服務器smtp.qq.com,對應端口號配置為25,這里的口令和帳號應替換為讀者自己的授權口令和帳號。該程序的正常結果如下:
1
2
|
[dechin@dechin - manjaro smtp]$ python3 smtp_test.py 郵件發送成功 |
另外由于這里采用了tracback做錯誤日志采集,因此即使有報錯程序也能繼續執行,但是會廣播錯誤日志。
最后通過查詢郵箱里面的郵件(有時候可能會被放到垃圾箱里面),正常情況下可以看到一份這樣的郵件:
使用crontab添加linux系統定時任務
crontab是linux系統下自帶的定時任務配置服務,基本使用方法就是通過crontab -l來查看定時任務,以及通過crontab -e來編輯定時任務。但是由于自帶的編輯器為nano,使用起來非常的不順手,所以我們可以將其編輯器配置為vim再進行使用,相關指令為:
[dechin@dechin-manjaro smtp]$ export editor="/usr/bin/vim" ; crontab -e
當然,在當前用戶登錄界面下,只需要臨時配置一次即可一直直接使用crontab -e進行配置,持久生效需要修改配置文件,這里不展開介紹。crontab的任務配置可以參考如下介紹(圖片來自于參考鏈接2):
一個定時任務+smtp廣播的示例
這里我們首先創建一個簡單的打印隨機數的任務,這樣如果我們在crontab中添加一個執行該程序的定時任務,就可以每次產生一個不同的隨機數并且將其輸出到一個指定的文件中,再通過另外一個smtp的定時任務進行讀取和廣播。以下是打印隨機數的任務內容:
1
2
3
|
[dechin@dechin - manjaro smtp]$ cat random_job.py import random print (random.random()) |
我們將前面用到的smtp的任務稍作修改,將隨機數讀取到郵件標題中:
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
|
# smtp_test.py #!/usr/bin/python # -*- coding: utf-8 -*- import smtplib from email.mime.text import mimetext from email.header import header # 第三方 smtp 服務 mail_host = "smtp.qq.com" #設置服務器 mail_pass = "passpasspasspass" #口令 message = mimetext( 'python smtp 郵件發送測試...' , 'plain' , 'utf-8' ) message[ 'from' ] = header( "smtp email" , 'utf-8' ) message[ 'to' ] = header( "test message" , 'utf-8' ) random_number = 1 with open ( '/home/dechin/projects/2021-python/smtp/random_number.txt' , 'r' ) as file : random_number = float ( file .readlines()[ 0 ]) subject = 'the random number generated is: ' + str (random_number) message[ 'subject' ] = header(subject, 'utf-8' ) try : smtpobj = smtplib.smtp() smtpobj.connect(mail_host, 25 ) # 25 為 smtp 端口號 smtpobj.login(mail_user,mail_pass) smtpobj.sendmail(sender, receivers, message.as_string()) print ( "郵件發送成功" ) except smtplib.smtpexception: import traceback traceback.print_exc() print ( "無法發送郵件" ) |
最后,再配置好crontab
定時任務如下:
1
2
3
|
[dechin@dechin - manjaro smtp]$ crontab - l * * * * * python3 / home / dechin / projects / 2021 - python / smtp / random_job.py > / home / dechin / projects / 2021 - python / smtp / random_number.txt * * * * * python3 / home / dechin / projects / 2021 - python / smtp / smtp_test.py |
上面由于為了盡快的展示定時任務效果因此我們設置為每分鐘都執行一次任務,實際場景中不需要這么高頻率的定時任務測試。
最后查看郵箱收件箱,我們發現了一系列的定時任務的內容反饋如下:
本文首發鏈接為:https://www.cnblogs.com/dechinphy/p/smtp.html
作者id:dechinphy
以上就是python調用smtp服務自動發送email的實現步驟的詳細內容,更多關于python調用smtp服務自動發送email的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/dechinphy/p/smtp.html