註冊一個簽名(用公衆號註冊方便)
創建模板(登錄、註冊等等)
創建一個應用(爲了使用AppID、AppKey)
from qcloudsms_py import SmsMultiSender, SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
import random #隨機生成n位數做驗證碼
def send_sms_single(phone_num, template_id, template_param_list):
"""
單條發送短信
:param phone_num: 電話號
:param template_id: 騰訊雲短信模板ID
:param template_param_list: 短信模板所需參數列表,例如:【驗證碼:{1},描述:{2}】,則傳遞參數 [888,666]按順序去格式化模板
:return:
"""
appid = APPID # 應用ID
appkey = APPKEY # 應用Key
sms_sign = SIGN # 騰訊雲創建簽名時填寫的簽名內容(使用公衆號的話這個值一般是公衆號全稱或簡稱)
sender = SmsSingleSender(appid, appkey)
try:
response = sender.send_with_param(86, phone_num, template_id, template_param_list, sign=sms_sign)
except HTTPError as e:
response = {'result': 1000, 'errmsg': "網絡異常發送失敗"}
return response
def send_sms_multi(phone_num_list, template_id, param_list):
"""
批量發送短信
:param phone_num_list:手機號列表
:param template_id:騰訊雲短信模板ID
:param param_list:短信模板所需參數列表,例如:【驗證碼:{1},描述:{2}】,則傳遞參數 [888,666]按順序去格式化模板
:return:
"""
appid = APPID # 應用ID
appkey = APPKEY # 應用Key
sms_sign = SIGN # 騰訊雲創建簽名時填寫的簽名內容(使用公衆號的話這個值一般是公衆號全稱或簡稱)
sender = SmsMultiSender(appid, appkey)
try:
response = sender.send_with_param(86, phone_num_list, template_id, param_list, sign=sms_sign)
except HTTPError as e:
response = {'result': 1000, 'errmsg': "網絡異常發送失敗"}
return response
if __name__ == '__main__':
result1 = send_sms_single(123456789,123,[666]) #單條發送
result2 = send_sms_multi([123456,456789], 789, [666, ]) #多條發送
#單個手機號是字符串,多個手機號是列表