timeshow / laravel-sms
Sms Api for Laravel
Requires
- php: ^8.1
- illuminate/http: ^9.0|^10.0
- illuminate/support: ^9.0|^10.0
Requires (Dev)
- phpunit/phpunit: ^9.3
README
This is a Laravel Package for SMS Gateway Integration. Now Sending SMS is easy.
List of supported gateways:
Install
Via Composer
$ composer require timeshow/laravel-sms
add the SmsServiceProvider
to your config/app.php
:
//providers 'providers' => [ // ... TimeShow\Sms\SmsServiceProvider::class, ] //aliases 'aliases' => [ //... 'Sms' => TimeShow\Sms\Facades\Sms::class, ]
Publish the sms configuration file.
php artisan vendor:publish --tag="sms"
In the config file you can set the default driver to use for all your SMS. But you can also change the driver at runtime.
Choose what gateway you would like to use for your application. Then make that as default driver so that you don't have to specify that everywhere. But, you can also use multiple gateways in a project.
Configure(.env)
//default driver SMS_DEFAULT=aliyun //fallback driver SMS_FALLBACK=juhe //default signName (Important: The signName should be enclosed in {}) SMS_SIGNNAME={sms} ALIYUN_APP_KEY=your-appkey ALIYUN_APP_SECRET=your-appsecret ALIYUN_TEMPLATE_ID=your-templates-id //Value:SMS_12345678 JUHE_KEY=your-key JUHE_TEMPLATE_ID=your-templates-id //Value:123456 YUNPIAN_API_KEY=your-appkey YUNPIAN_TEMPLATE_CONTENT=your-template-content //Value:您的验证码是{verifyCode},有效期为{time}分钟,请尽快验证 QQYUN_APP_ID=your-appid QQYUN_APP_KEY=your-appkey QQYUN_TEMPLATE_ID=your-templates-id //Value:12345
Then fill the credentials for that gateway in the drivers array.
/* |-------------------------------------------------------------------------- | Default Driver |-------------------------------------------------------------------------- */ 'default' => env('SMS_DRIVER', 'aliyun'), 'fallback' => env('SMS_FALLBACK', 'juhe'), 'signName' => env('SMS_SIGNNAME', ''), /* |-------------------------------------------------------------------------- | List of Drivers |-------------------------------------------------------------------------- */ 'drivers' => [ // Install: composer require 'aliyun' => [ 'appKey' => env('ALIYUN_APP_KEY', 'Your Access Key'), 'appSecret' => env('ALIYUN_APP_SECRET', 'Your Secret Key'), 'templateId' => env('ALIYUN_TEMPLATE_ID', 0), 'driverFile' => 'ALiYun', ], 'juhe' => [ 'key' => env('JUHE_KEY', 'Your Access Key'), 'templateId' => env('JUHE_TEMPLATE_ID', 0), 'driverFile' => 'JuHe', ], 'yunpian' => [ 'apiKey' => env('YUNPIAN_API_KEY'), 'templateContent' => env('YUNPIAN_TEMPLATE_CONTENT'), 'driverFile' => 'YunPian', ], 'qqyun' => [ 'appId' => env('QQYUN_APP_ID', 'Your App Id'), 'appKey' => env('QQYUN_APP_KEY', 'Your App Key'), 'templateId' => env('QQYUN_TEMPLATE_ID', 0), 'executableFile' => 'QQYun', ], ],
Basic Usage
use it like this
//default $sms = Sms::driver(); //fallback $sms = Sms::driver('fallback'); //other options $sms = Sms::driver('juhe'); $sms = Sms::driver('aliyun'); $sms = Sms::driver('yunpian');
Usage
$sms = Sms::driver(); $sms->setTemplateId(123456); $sms->setSignName('your_signName'); $sms->setContent($content); $sms->setContentByVerifyCode(20); //Your verification code is {verifyCode}, valid for 20 minutes $sms->makeStr(); // Generate a 16 bit default random string $sms->makeCode(6); // 100000~999999 1000~9999 10000000~99999999 $sms->makeRandom(); // 100000~999999 $templateVar = ['code' => $verifyCode]; // ['code' => '110101', 'time' =>'10'] $sms->setTemplateVar($templateVar); $sms->setTemplateVar($templateVar, true); $sms->send($mobile); $sms->send($mobile, false);
Basic Usage
// JuHe $sms->setContent('#code#=' . $code); // ALiYun & QQYun $templateVar = ['code' => $code]; // YunPian $sms->setContentByVerifyCode();
Example
// Custom $sms = Sms::driver(); $default = config('sms.default'); $templateId = config('sms.drivers.' . $default . '.templateId'); $sms->setTemplateId($templateId); $content = 'Your verification code is {verifyCode}, Valid for {time} minutes'; $sms->setContent($content); $result = $sms->send($mobile); //Or JuHe $sms = Sms::driver(); $default = config('sms.default'); $templateId = config('sms.drivers.' . $default . '.templateId'); $sms->setTemplateId($templateId); $code = $sms->makeCode(6); $sms->setContent('#code#='.$code); $result = $sms->send($mobile, true); //Or ALiYun $sms = Sms::driver(); $default = config('sms.default'); $templateId = config('sms.drivers.' . $default . '.templateId'); $sms->setTemplateId($templateId); $code = $sms->makeCode(6); $templateVar = ['code' => $code]; $sms->setTemplateVar($templateVar, true); $result = $sms->send($mobile, true); //Or YunPian $sms = Sms::driver(); $templateVar = ['yzm' => 'verifyCode']; $sms->setTemplateVar($templateVar, true); $sms->setContentByVerifyCode(20); $result = $sms->send($mobile); //Or Set Content By Custom $sms = Sms::driver(); $sms->setSignName('your_signName'); $content = '{name},Your account is logged in from another location. If it was not for you, please change the password in a timely manner'; //content $templateVar = ['name' => 'you name']; $sms->setContent($content); $sms->setContentByCustomVar($templateVar); //Value:you name,Your account is logged in from another location. If it was not for you, please change the password in a timely manner //Or QQYun $sms = Sms::driver(); $default = config('sms.default'); $templateId = config('sms.drivers.' . $default . '.templateId'); $sms->setTemplateId($templateId); $code = $sms->makeCode(6); $templateVar = ['code' => $code]; $sms->setTemplateVar($templateVar, true); $result = $sms->send($mobile, true);
Security
If you discover any security related issues, please email 397975896@qq.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.