This is a Laravel 5 Package for SMS Gateway Integration. This package supports Laravel 5.2 or Higher
. Now Sending SMS is easy.
List of supported gateways:
- AWS SNS
- Nexmo
- Textlocal
- Twilio
- Clockwork
- LINK Mobility
- Kavenegar
- Melipayamak
- Smsir
- Tsms
- Farazsms
- Others are under way.
Via Composer
$ composer require tzsk/sms
If you are using Laravel 5.5
or higher then you don't need to add the provider and alias.
In your config/app.php
file add these two lines.
# In your providers array.
'providers' => [
...
Tzsk\Sms\Provider\SmsServiceProvider::class,
],
# In your aliases array.
'aliases' => [
...
'Sms' => Tzsk\Sms\Facade\Sms::class,
],
Now run php artisan vendor:publish
to publish config/sms.php
file in your config directory.
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.
Chose 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.
// Eg. if you wan to use SNS.
'default' => 'sns',
Then fill the credentials for that gateway in the drivers array.
// Eg. for SNS.
'drivers' => [
'sns' => [
// Fill all the credentials here.
'key' => 'Your AWS SNS Access Key',
'secret' => 'Your AWS SNS Secret Key',
'region' => 'Your AWS SNS Region',
'sender' => 'Your AWS SNS Sender ID',
'type' => 'Tansactional', // Or: 'Promotional'
],
...
]
Textlocal is added by default. You just have to change the creadentials in the textlocal
driver section.
In case you want to use AWS SNS. Then you have to pull a composer library first.
composer require aws/aws-sdk-php
In case you want to use Nexmo. Then you have to pull a composer library first.
composer require nexmo/client
In case you want to use Clockwork. Then you have to pull a composer library first.
composer require mediaburst/clockworksms
In case you want to use Twilio. Then you have to pull a composer library first.
composer require twilio/sdk
Then you just have to change the creadentials in the twilio
driver section.
In case you want to use Melipayamak. Then you have to pull a composer library first.
composer require melipayamak/php
In case you want to use Kavenegar. Then you have to pull a composer library first.
composer require kavenegar/php
In your code just use it like this.
# On the top of the file.
use Tzsk\Sms\Facade\Sms;
...
# In your Controller.
Sms::send("this message", function($sms) {
$sms->to(['Number 1', 'Number 2']); # The numbers to send to.
});
# OR...
Sms::send("this message")->to(['Number 1', 'Number 2'])->dispatch();
# If you want to use a different driver.
Sms::via('gateway')->send("this message", function($sms) {
$sms->to(['Number 1', 'Number 2']);
});
# OR...
Sms::via('gateway')->send("this message")->to(['Number 1', 'Number 2'])->dispatch();
# Here gateway is explicit : 'twilio' or 'textlocal' or any other driver in the config.
# The numbers can be a single string as well.
# If you are not a Laravel's facade fan, you can use sms helper:
sms()->send("this message", function($sms) {
$sms->to(['Number 1', 'Number 2']); # The numbers to send to.
});
sms()->send("this message")->to(['Number 1', 'Number 2'])->dispatch();
sms()->via('gateway')->send("this message", function($sms) {
$sms->to(['Number 1', 'Number 2']);
});
sms()->via('gateway')->send("this message")->to(['Number 1', 'Number 2'])->dispatch();
First you have to create your notification using php artisan make:notification
command.
then SmsChannel::class
can be used as channel like the below:
namespace App\Notifications;
use Tzsk\Sms\SmsBuilder;
use Illuminate\Bus\Queueable;
use Tzsk\Sms\Channels\SmsChannel;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
class InvoicePaid extends Notification
{
use Queueable;
/**
* Get the notification channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return [SmsChannel::class];
}
/**
* Get the repicients and body of the notification.
*
* @param mixed $notifiable
* @return SmsBuilder
*/
public function toSms($notifiable)
{
return (new SmsBuilder)->via('gateway') # via() is Optional
->send('this message')
->to('some number');
}
}
Tip: You can use the same Builder Instance in the send method.
$builder = (new SmsBuilder)->via('gateway') # via() is Optional
->send('this message')
->to('some number');
Sms::send($builder);
# OR...
$builder = (new SmsBuilder)->send('this message')
->to(['some number']);
Sms::via('gateway')->send($builder);
First you have to name your driver in the drivers array and also you can specify any config params you want.
'drivers' => [
'textlocal' => [...],
'twilio' => [...],
'my_driver' => [
... # Your Config Params here.
]
]
Now you have to create a Driver Map Class that will be used to send the SMS.
In your driver, You just have to extend Tzsk\Sms\Abstracts\Driver
.
Ex. You created a class : App\Packages\SMSDriver\MyDriver
.
namespace App\Packages\SMSDriver;
use Tzsk\Sms\Abstracts\Driver;
class MyDriver extends Driver
{
# You will have to make 2 methods.
/**
* 1. __constructor($settings) # {Mandatory} This settings is your Config Params that you've set.
* 2. send() # (Mandatory) This is the main message that will be sent.
*
* Example Given below:
*/
/**
* @var object
*/
protected $settings;
/**
* @var mixed
*/
protected $client;
/**
* Your Driver Config.
*
* @var array $settings
*/
public function __construct($settings)
{
$this->settings = (object) $settings;
# Initialize any Client that you want.
$this->client = new Client(); # Guzzle Client for example.
}
/**
* @return object Ex.: (object) ['status' => true, 'data' => 'Client Response Data'];
*/
public function send()
{
$this->recipients; # Array of Recipients.
$this->body; # SMS Body.
# Main logic of Sending SMS.
...
}
}
Once you crate that class you have to specify it in the sms.php
Config file map
section.
'map' => [
...
'my_driver' => App\Packages\SMSDriver\MyDriver::class,
]
Note:- You have to make sure that the key of the map
array is identical to the key of the drivers
array.
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.