安装依赖
pip install aliyun-python-sdk-core-v3
sdk依赖安装
pip install alibabacloud_dysmsapi20170525==2.0.2
如果冲突 , 删除版本信息
settings.py
# 阿里短信
ACCESS_KEY_ID = ""
ACCESS_KEY_SECRET = ""
ALIYUN_SMS_SIGN_NAME = ""
SMS_CODE = ""
sms.py
try:
reload(sys)
sys.setdefaultencoding('utf8')
except NameError:
pass
except Exception as err:
raise err
# 注意:不要更改
REGION = "cn-hangzhou"
PRODUCT_NAME = "Dysmsapi"
DOMAIN = "dysmsapi.aliyuncs.com"
acs_client = AcsClient(settings.ACCESS_KEY_ID, settings.ACCESS_KEY_SECRET, REGION)
region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)
# 发送验证码
def send_sms(
business_id,
phone_numbers,
template_code=settings.SMS_CODE,
sign_name=settings.ALIYUN_SMS_SIGN_NAME,
template_param=None
):
smsRequest = SendSmsRequest.SendSmsRequest()
smsRequest.set_TemplateCode(template_code)
if template_param is not None:
smsRequest.set_TemplateParam(template_param)
smsRequest.set_OutId(business_id)
smsRequest.set_SignName(sign_name)
smsRequest.set_PhoneNumbers(phone_numbers)
smsResponse = acs_client.do_action_with_exception(smsRequest)
return smsResponse
views.py
__business_id = uuid.uuid1()
sms.send_sms(__business_id, phone, template_param=code)