We often have the need to send automated emails in applications, whether it’s sending confirmation emails, transactional emails or emails for marketing campaigns. The process of setting up these automated emails has become incredibly easy to implement in most of the common web platforms.
How about wanting to take client notifications that one step further by automating SMS notifications? Well in this tutorial I will show how we can easily set this up in a Laravel 5 application, I will be using the Nexmo API Service which integrates with Laravel very easily. (The reason I’m choosing to write articles around the Laravel framework is purely based on preference, I find it very easy to write code that hooks into third party API’s in Laravel and it takes even less time to do so when there are useful wrapper packages available).
Sending SMS notifications in Laravel
We first need to add the Nexmo PHP client library to our project using Composer:
composer require nexmo/laravel
We then need to add Nexmo\Laravel\NexmoServiceProvider to the providers array in your config/app.php:
If you want to use the facade interface, you can use the facade class when needed:
Or add an alias in your config/app.php:
'Nexmo' => \Nexmo\Laravel\Facade\Nexmo::class
You can use artisan vendor:publish to copy the distribution configuration file to your app’s config directory:
php artisan vendor:publish
Then update config/nexmo.php with your credentials. You can also update your .env file with the following:
Now for the fun part, to actually send SMS messages using Nexmo you can use the Facade we added earlier like this:
'to' => '14845551244',
'from' => '16105552344',
'text' => 'Using the facade to send a message.'
It’s as simple as that, we can now send SMS notifications to any number with a simple method call from anywhere in our application.
Ofcourse using Nexmo is not limited to just Laravel, you can use the PHP client library in any framework of your choice, if you don’t use PHP they also have libraries for Node, C#, Python, Java and Ruby. If you want to test it out first, Nexmo give you some free credit that allows you to send up to 10 free sms notifications.
You’re wondering about their pricing right? Nexmo charge €0.0333 per message delivered. It is free to receive messages.
Overall I’d recommend this service to anyone who is in the need of sending automated transactional sms notifications to their clients from their application as it was so easy to set up and works like a charm.
That’s all folks.