# SMS

### 1. 使用说明

```
1. 注册阿里云账户：如果你还没有阿里云账户，需要先注册一个。
2. 开通短信服务：登录到阿里云控制台，找到“短信服务”，并根据提示开通服务。
3. 获取 AccessKey ID 和 AccessKey Secret：
   · 这两个凭证用于验证你的身份，你可以在阿里云控制台的“访问控制”页面上创建和查看它们。
   · 请妥善保管这些凭证，不要泄露给他人。
4. 安装 SDK：阿里云提供了 Python SDK，用于简化 API 的调用。
5. 封装短信发送代码 sms_sender.py
6. 导入短信发送函数，传参调用
```

### 2. 安装阿里云 Python SDK

```bash
pip3 install aliyun-python-sdk-core
```

### 3. sms\_sender.py

```python
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

def send_sms(phone_number, message):
    access_key_id = "你的AccessKeyID"
    access_key_secret = "你的AccessKeySecret"
    
    client = AcsClient(access_key_id, access_key_secret, 'cn-hangzhou')

    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https') 
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers', phone_number)
    request.add_query_param('SignName', "短信签名")
    request.add_query_param('TemplateCode', "短信模板代码")
    request.add_query_param('TemplateParam', message)

    response = client.do_action_with_exception(request)
    print(str(response, encoding='utf-8'))

```

### 4. main.py

```python
from sms_sender import send_sms

# 替换为目标手机号和你的消息
phone_number = "目标手机号"
message = "{\"code\":\"12345\"}"  # 以 JSON 格式提供短信模板中的变量

# 发送短信
send_sms(phone_number, message)

```

### 代码备份

```python
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

def send_sms(message):
    access_key_id = "7TAI5tCW37HoZHrmZWnMV14L"
    access_key_secret = "RgO2QbJu6pR3gE4oGSckI37u6R5cUr"
    
    client = AcsClient(access_key_id, access_key_secret, 'cn-hangzhou')

    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https') 
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers', '15676752369')
    request.add_query_param('SignName', "EXD")
    request.add_query_param('TemplateCode', "SMS_464220777")
    request.add_query_param('TemplateParam', message)

    response = client.do_action_with_exception(request)
    #print(str(response, encoding='utf-8'))
```

#### 使用socks5代理

```python
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

import socks
import socket


def send_sms(message):
    # 配置 Socks5 代理
    socks.set_default_proxy(socks.SOCKS5, "8.1.2.229", 9000, username='dante', password='cat pw')
    socket.socket = socks.socksocket

    access_key_id = "7TAI5tCW37HoZHrmZWnMV14L"
    access_key_secret = "RgO2QbJu6pR3gE4oGSckI37u6R5cUr"
    
    client = AcsClient(access_key_id, access_key_secret, 'cn-hangzhou')

    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https') 
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers', '15888852369')
    request.add_query_param('SignName', "EXD")
    request.add_query_param('TemplateCode', "SMS_464220777")
    request.add_query_param('TemplateParam', message)

    response = client.do_action_with_exception(request)

    with open('./sms_result.txt', 'a') as f:
        f.write(str(response, encoding='utf-8') + '\n')
```
