yudina/laravel-sms

0.0.7 2019-11-12 13:49 UTC

This package is auto-updated.

Last update: 2024-10-28 19:28:59 UTC


README

This package makes it easy to send SMS notifications for some sms providers with Laravel 5.8.

Supported SMS providers:

Installation

Add SmsNotification package to your Laravel project via composer:

composer required yudina/laravel-sms

Then publish configuration file:

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

Settings up your account for select sms provider

Add the environment variables to your 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')
    ],
    ...
]
...

Add your necessary keys to your .env:

 // .env
 SMSRU_API_ID=
 ...

Usage

Now you can use the channel in your via() method inside the notification:

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;
    }
}