An SMS wrapper for major sms gateways

v2.1.7 2019-08-14 06:21 UTC

README

Latest Stable Version Latest Unstable Version License Total Downloads

Introduction

Elimuswift SMS is a package for sending SMS using various SMS providers. This package for Laravel adds the capability to send and receive SMS/MMS messages to mobile phones from your web app. The package supports Africas Talking

Requirements

Laravel 5

  • PHP: >= 5.5
  • Guzzle >= 6.0

Configuration

Composer

First, add the Simple SMS package to your require in your composer/json file:

"require": {
    "elimuswift/sms": "~2.0"
}

Next, run the composer update command. This will install the package into your Laravel application.

Service Provider

Once you have added the package to your composer file, you will need to register the service provider with Laravel.

	Elimuswift\SMS\Providers\SmsServiceProvider::class,

Aliases

Finally, register the Facade.

'SMS' => Elimuswift\SMS\Facades\SMS::class,

API Settings

You must run the following command to save your configuration files to your local app:

 php artisan vendor:publish --provider="Elimuswift\SMS\Providers\SmsServiceProvider"

This will copy the configuration files to your config folder.

Get Started

Africas Talkig

To enable the AfricasTalking driver just change config file to:

'driver' => env('SMS_DRIVER', 'africastalking'),
'africastalking' => [
        'api_key' => env('AT_API_KEY', 'africastalking.api_key'),
        'username' => env('AT_USERNAME', 'africastalking.username'),
    ]

Nexmo Driver

This driver sends messages through the Nexmo messaging service. It is very reliable and capable of sending messages to mobile phones worldwide.

    return [
        'driver' => 'nexmo',
        'from' => 'Company Name',
        'nexmo' => [
            'api_key'       => 'Your Nexmo API Key',
            'api_secret'    => 'Your Nexmo API Secret',
            'encoding'      => 'unicode', // Can be `unicode` or `gsm`
        ]
    ]; 

Twilio Driver

This driver sends messages through the Twilio messaging service. It is very reliable and capable of sending messages to mobile phones worldwide.

    return [
        'driver' => 'twilio',
        'from' => '+15555555555', //Your Twilio Number in E.164 Format.
        'twilio' => [
            'account_sid' => 'Your SID',
            'auth_token' => 'Your Token',
            'verify' => true,  //Used to check if messages are really coming from Twilio.
        ]
    ];

Sending an SMS

With everything set up the right way sending an SMS notification would be as simple as:

use Elimuswift\SMS\Facades\SMS;

SMS::send('My first SMS message', [], function ($sms) {
	$sms->to('07xxxxxxxx');
}); 

Multiple Recipients

Sending to multiple Contacts

use Elimuswift\SMS\Facades\SMS;
$contacts = ['0711xxxxxx', '0722xxxxxx', '0701xxxxxx'];

SMS::send('My bulk SMS notification', [], function ($sms) use($contacts) {
	return array_map(function ($to) use($sms) {
		$sms->to($to);
	}, $contacts);	
}); 

Send a Blade View

You can also use a view to send the sms notification, The first parameter is the view file that you would like to use. The second is the data that you wish to pass to the view. The final parameter is a callback that will set all of the options on the message closure.

use App\Order;

$order = Order::with('user')->first();

SMS::send('sms.order-shiped', compact('order'), function($sms) use($order) {
    $sms->to($order->user->phone_number);
});

Driver

The driver method will switch the provider during runtime.

//Will send through default provider set in the config file.
SMS::send('Your SMS Message', [], function($sms) {
    $sms->to('+15555555555');
});

SMS::driver('nexmo');

//Will send through Nexmo
SMS::send('Your SMS Message', [], function($sms) {
    $sms->to('+15555555555');
});

Using Laravel Notifications

The package comes with a notification chanel for sending SMS messages using laravels notification system. To get stated add routeNotificationForSMS() method in your notifiable. this method should return the notifiables's phone number.

    /**
     * Get the notification identifier for SMS.
     *
     * @return string
     */
    public function routeNotificationForSMS()
    {
        return $this->attributes['phone_number'];
    }

Sending The Notification

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

use Elimuswift\SMS\Chennels\SMSChannel;
use Elimuswift\SMS\Notifications\SMSMessage;
use Illuminate\Notifications\Notification;

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

    public function toSms($notifiable)
    {
        return (new SMSMessage())
            ->content("Your {$notifiable->service} account was approved!");
    }
}

You can also send a notification as a blade view.

public function toSms($notifiable)
{
    return (new SMSMessage('path.to.view'))
         ->viewData(['foo' => 'Bar', 'baaz' => 'blah']);
}