yudina/laravel-sms

短信服务提供商

0.0.7 2019-11-12 13:49 UTC

This package is auto-updated.

Last update: 2024-09-28 19:20:45 UTC


README

此包使您能够轻松使用Laravel 5.8向一些短信服务提供商发送短信通知。

支持的短信服务提供商

安装

通过composer将SmsNotification包添加到您的Laravel项目中

composer required yudina/laravel-sms

然后发布配置文件

php artisan vendor:publish --provider="Yudina\LaravelSms\SmsSenderServiceProvider"

为所选短信服务提供商设置账户

将环境变量添加到您的config/services.php中

// config/services.php
...
'default' => env('SMS_PROVIDER', 'smscru'),

'providers' => [
    'smsru' => [
        'api_id' => env('SMSRU_API_ID')
    ],
    'smscru' => [
        'login' => env('SMSCRU_LOGIN'),
        'password' => env('SMSCRU_PASSWORD'),
        'sender' => env('SMSCRU_SENDER')
    ],
    ...
]
...

将必要的密钥添加到您的.env中

 // .env
 SMSRU_API_ID=
 ...

用法

现在您可以在通知中的via()方法中使用该通道

use Yudina\LaravelSms\SmsSenderChannel;
use Yudina\LaravelSms\SmsSenderMessage;

use Illuminate\Notifications\Notification;

class CodeGenerationNotification extends Notification
{
    public function via($notifiable)
    {
        return [SmsSenderChannel::class];
    }

    public function toSms($notifiable)
    {
        $message = 'Activation code: ' . $this->generateCode(6);
    
        return new SmsSenderMessage($message);
    }
    
    private function generateCode(int $length): string
    {
        $result = '';

        for($i = 0; $i < $length; $i++)
            $result .= mt_rand(0, 9);

        return $result;
    }
}