Python3實現發送郵件:
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
|
import smtplib from email.mime.text import MIMEText from email.utils import formataddr my_pass = '*********' # 發件人郵箱的授權碼 def mail(): ret = True try : msg = MIMEText( '驗證碼為:123456' , 'plain' , 'utf-8' ) msg[ 'From' ] = formataddr([ "From nicead.top" , my_sender]) # 括號里的對應發件人郵箱昵稱、發件人郵箱賬號 msg[ 'To' ] = formataddr([ "FK" , my_user]) # 括號里的對應收件人郵箱昵稱、收件人郵箱賬號 msg[ 'Subject' ] = "驗證碼" # 郵件的主題,也可以說是標題 server = smtplib.SMTP_SSL( "smtp.qq.com" , 465 ) # 發件人郵箱中的SMTP服務器,端口是25 server.login(my_sender, my_pass) # 括號中對應的是發件人郵箱賬號、郵箱密碼 server.sendmail(my_sender, [my_user, ], msg.as_string()) # 括號中對應的是發件人郵箱賬號、收件人郵箱賬號、發送郵件 server.quit() # 關閉連接 except Exception as e: # 如果 try 中的語句沒有執行,則會執行下面的ret=False ret = False return ret ret = mail() if ret: print ( "郵件發送成功" ) else : print ( "郵件發送失敗" ) |
注意授權碼:需要在郵件中的設置中獲取,如QQ郵箱獲取授權碼的方法:
Python3實現短信驗證碼
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
|
# 接口類型:互億無線觸發短信接口,支持發送驗證碼短信、訂單通知短信等。 # 賬戶注冊:請通過該地址開通賬戶http://sms.ihuyi.com/register.html # 注意事項: # (1)調試期間,請使用用系統默認的短信內容:您的驗證碼是:【變量】。請不要把驗證碼泄露給其他人。; # (2)請使用APIID(查看APIID請登錄用戶中心->驗證碼短信->產品總覽->APIID)及 APIkey來調用接口; # (3)該代碼僅供接入互億無線短信接口參考使用,客戶可根據實際需要自行編寫; #發送短信 #APIID:C11345804 #APIKEY:735d183ae02189f678c26800ac19b03a # !/usr/local/bin/python # -*- coding:utf-8 -*- import http.client import urllib host = "106.ihuyi.com" sms_send_uri = "/webservice/sms.php?method=Submit" # 用戶名是登錄用戶中心->驗證碼短信->產品總覽->APIID account = "************" # 密碼 查看密碼請登錄用戶中心->驗證碼短信->產品總覽->APIKEY password = "**********************" def send_sms(text, mobile): params = urllib.parse.urlencode( { 'account' : account, 'password' : password, 'content' : text, 'mobile' : mobile, 'format' : 'json' }) headers = { "Content-type" : "application/x-www-form-urlencoded" , "Accept" : "text/plain" } conn = http.client.HTTPConnection(host, port = 80 , timeout = 30 ) conn.request( "POST" , sms_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_str if __name__ = = '__main__' : #需要接受短信的目標手機號 mobile = "手機號" #短信內容 text = "您的驗證碼是:121254。請不要把驗證碼泄露給其他人。" ret = send_sms(text, mobile).decode( 'utf-8' ) import json ret = json.loads(ret) print (ret) |
注意:
1
2
3
4
|
# 用戶名是登錄用戶中心->驗證碼短信->產品總覽->APIID account = "************" # 密碼 查看密碼請登錄用戶中心->驗證碼短信->產品總覽->APIKEY password = "**********************" |
需要在互億無線觸發短信接口的官網注冊一個賬號,會提供免費發送十次短信驗證碼。http://sms.ihuyi.com/register.html
總結
以上所述是小編給大家介紹的Python3實現發送郵件和發送短信驗證碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
原文鏈接:https://www.cnblogs.com/yunwangjun-python-520/p/11153833.html