安装依赖

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)
End
文章作者:
文章地址:http://honglvliyu.cn/index.php/archives/32/
文章标题:阿里云短信
文章版权:本文由Beyond the game独立整理撰写,转载请保留文章出处。
Last modification:April 24th, 2023 at 10:07 am
咋滴 看了不给钱呀
广而告知