python3实现阿里云短信发送功能

# -*- coding: utf-8 -*-
import uuid
import sys
import json
import uuid
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.profile import region_provider
from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest
# 注意:不要更改
REGION = "cn-hangzhou"
PRODUCT_NAME = "Dysmsapi"
DOMAIN = "dysmsapi.aliyuncs.com"
ACCESSKEY = '你的KEY'
ACCESS_SECRET = '你的SECRET'
class AliSms:

    @staticmethod
    def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None):
        acs_client = AcsClient(ACCESSKEY, ACCESS_SECRET, REGION)
        region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)
        sms_request = SendSmsRequest.SendSmsRequest()
        # 申请的短信模板编码,必填
        sms_request.set_TemplateCode(template_code)
        # 短信模板变量参数
        if template_param is not None:
            sms_request.set_TemplateParam(template_param.encode('utf-8'))
        # 设置业务请求流水号,必填。
        sms_request.set_OutId(business_id)
        # 短信签名
        sms_request.set_SignName(sign_name.encode('utf-8'))
        sms_request.set_PhoneNumbers(phone_numbers.encode('utf-8'))
        # 调用短信发送接口,返回json
        sms_response = acs_client.do_action_with_exception(sms_request)
        return sms_response.decode('utf-8')

sms_result = AliSms.send_sms( uuid.uuid1(), '13655699934', "智加数字科技", "SMS_271405300", json.dumps({'text': '测试', 'time': '2023-09-12 12:30:30', 'place': '安徽省合肥市'}))

  

 

 


posted @ 2023-03-26 21:18  何双新  阅读(305)  评论(0编辑  收藏  举报