Python3實現傳送郵件和傳送簡訊驗證碼功能
阿新 • • 發佈:2020-01-09
Python3實現傳送郵件:
import smtplib from email.mime.text import MIMEText from email.utils import formataddr my_sender = '[email protected]' # 發件人郵箱賬號 my_pass = '*********' # 發件人郵箱的授權碼 my_user = '[email protected]' # 收件人郵箱賬號,我這邊傳送給自己 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實現簡訊驗證碼
# 介面型別:互億無線觸發簡訊介面,支援傳送驗證碼簡訊、訂單通知簡訊等。 # 賬戶註冊:請通過該地址開通賬戶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)
注意:
# 使用者名稱是登入使用者中心->驗證碼簡訊->產品總覽->APIID account = "************" # 密碼 檢視密碼請登入使用者中心->驗證碼簡訊->產品總覽->APIKEY password = "**********************"
需要在互億無線觸發簡訊介面的官網註冊一個賬號,會提供免費傳送十次簡訊驗證碼。http://sms.ihuyi.com/register.html
總結
以上所述是小編給大家介紹的Python3實現傳送郵件和傳送簡訊驗證碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!