Easy SMS

repository·master·Indexed 25 days ago

https://github.com/overtrue/easy-sms

A multi-channel SMS sending component for PHP (>= 8.4) that provides a unified interface for various SMS service providers. It supports multiple gateways including Aliyun, Chuanglan, Huawei Cloud, NetEase Yunxin, Volcengine, Twilio, and Tiniyo. Features include gateway calling strategies, support for international numbers via the PhoneNumber class, custom gateway extensions, and reusable message models by extending the Message class.

Tokens
7.4K
Snippets
17
Records
45
Agent score
85%

What's inside easy-sms

  1. Configure and use Easy SMS

    master

    Initialize EasySms with a configuration array. The configuration includes a timeout for HTTP requests, a default section to define the gateway calling strategy and the list of gateways to use, and a gateways section containing the specific credentials for each provider.

    To send a message, use the send method with the recipient's phone number and an array containing content, template, and data.

    use Overtrue//EasySms\EasySms;
    
    $config = [
        // HTTP request timeout in seconds
        'timeout' => 5.0,
    
        // Default sending configuration
        'default' => [
            // Gateway calling strategy, default: OrderStrategy
            'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
    
            // Default available gateways
            'gateways' => [
                'yunpian', 'aliyun',
            ],
        ],
        // Configuration for available gateways
        'gateways' => [
            'errorlog' => [
                'file' => '/tmp/easy-sms.log',
            ],
            'yunpian' => [
                'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
            ],
            'aliyun' => [
                'access_key_id' => '',
                'access_key_secret' => '',
                'sign_name' => '',
            ],
            //...
        ],
    ];
    
    $easySms = new EasySms($config);
    
    $easySms->send(13188888888, [
        'content'  => '您的验证码为: 6379',
        'template' => 'SMS_001',
        'data' => [
            'code' => 6379
        ],
    ]);
  2. Configure Twilio and Tiniyo Gateways

    master

    Both Twilio and Tiniyo require the recipient's phone number to include the country code prefixed with a + sign.

    // Twilio
    'twilio' => [
        'account_sid' => '',
        'from' => '',
        'token' => '',
    ],
    
    // Tiniyo
    'tiniyo' => [
        'account_sid' => '',
        'from' => '',
        'token' => '',
    ],
  3. Configure NetEase Yunxin SMS

    master

    NetEase Yunxin supports both verification codes and notification templates.

    • Verification Codes: Uses template and data. You can set code_length (4-10, default 4) in config. In send(), setting action to sendCode (default) or verifyCode is supported.
    • Notification Templates: Uses template and data. Set action to sendTemplate and provide parameters in the params array.
    'yunxin' => [
        'app_key' => '',
        'app_secret' => '',
        'code_length' => 4,
        'need_up' => false,
    ],
  4. Configure Aliyun SMS Gateways

    master

    Aliyun provides several gateway options. Most use template and data for content.

    • Aliyun (Standard): Requires access_key_id, access_key_secret, and sign_name.
    • Aliyun Rest: Requires app_key, app_secret_key, and sign_name.
    • Aliyun International: Requires access_key_id, access_key_secret, and sign_name.
    • Aliyun SMS Authentication (DYPNS): Requires access_key_id, access_key_secret, and sign_name. Note that sign_name cannot be customized for this service.
    // Aliyun Standard
    'aliyun' => [
        'access_key_id' => '',
        'access_key_secret' => '',
        'sign_name' => '',
    ],
    
    // Aliyun Rest
    'aliyunrest' => [
        'app_key' => '',
        'app_secret_key' => '',
        'sign_name' => '',
    ],
    
    // Aliyun International
    'aliyunintl' => [
        'access_key_id' => '',
        'access_key_secret' => '',
        'sign_name' => '',
    ],
    
    // Aliyun SMS Authentication (DYPNS)
    'aliyundypns' => [
        'access_key_id' => '',
        'access_key_secret' => '',
        'sign_name' => '',
    ],
  5. Configure Chuanglan (253 Cloud) Gateways

    master

    Chuanglan offers two main modes:

    1. Standard (Chuanglan): Uses content. Requires account, password, and optional intel_account/intel_password for international SMS. Use the channel option to switch between CHANNEL_VALIDATE_CODE (default) and CHANNEL_PROMOTION_CODE.
    2. Chuanglan V1:
      • Normal SMS: Uses content. Requires account, password, and channel set to CHANNEL_NORMAL_CODE.
      • Variable SMS: Uses template and data. Requires channel set to CHANNEL_VARIABLE_CODE and data formatted as a specific string.
    // Chuanglan (253 Cloud)
    'chuanglan' => [
        'account' => '',
        'password' => '',
        'intel_account' => '',
        'intel_password' => '',
        'channel'  => \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_VALIDATE_CODE,
        'sign' => '【通讯云】',
        'unsubscribe' => '回TD退订',
    ],
    
    // Chuanglan V1 (Normal)
    'chuanglanv1' => [
        'account' => '',
        'password' => '',
        'needstatus' => false,
        'channel' => \Overtrue\EasySms\Gateways\Chuanglanv1Gateway::CHANNEL_NORMAL_CODE,
    ],
    
    // Chuanglan V1 (Variable)
    'chuanglanv1' => [
        'account' => '',
        'password' => '',
        'needstatus' => false,
        'channel' => \Overtrue\EasySms\Gateways\Chuanglanv1Gateway::CHANNEL_VARIABLE_CODE,
    ],
  6. Configure Volcengine SMS

    master

    Volcengine SMS uses template and data. You can override configuration settings like sign_name, sms_account, and phone_numbers (for batch sending) by passing them in the data array during the send call.

    'volcengine' => [
        'access_key_id' => '',
        'access_key_secret' => '',
        'region_id' => 'cn-north-1',
        'sign_name' => '',
        'sms_account' => '',
    ],
  7. Configure Huawei Cloud SMS

    master

    Huawei Cloud SMS uses template and data. You can define multiple signature channels in the from configuration array. You can specify which channel to use by passing the corresponding key in the data array during the send call.

    'huawei' => [
        'endpoint' => '',
        'app_key' => '',
        'app_secret' => '',
        'from' => [
            'default' => '1069012345',
            'custom' => 'csms12345',
            'abc' => 'csms67890',
        ],
        'callback' => ''
    ],
  8. Use ChuanglanGateway for International SMS

    master

    The ChuanglanGateway automatically switches to the international endpoint (http://intapi.253.com/send/json) if the recipient's IDD code is not 86.

    When sending international SMS, it is recommended to provide intel_account and intel_password in your configuration, though it will fall back to your standard account and password if they are not explicitly set.

  9. Send SMS with specific signature channel on Huawei Cloud

    master

    To use a non-default signature channel on Huawei Cloud, include the from key in the data array, matching a key defined in your configuration.

    $easySms->send(13188888888, [
        'template' => 'SMS_001',
        'data' => [
            6379,
            'from' => 'custom' // Matches 'custom' key in config
        ],
    ]);