How to send sms notification to mobile phone with laravel 5.5

Published Date: 25-Nov-2017 | Tags: Laravel 5.5  Email & SMS  

This tutorial I want to show you how to send sms to mobile phone by using laravel 5 and nexmo.

Step 1: Install nexmo/client package.

To install it, please go to terminal or command prompt (for window users), navigate to your project directory, and run this command.

composer require nexmo/client

 

Step 2: Register Nexmo Account

Go to nexmo website https://www.nexmo.com and register nexmo account. You can register trail account for testing.

nexmo

Then we will use the key and secret in Laravel SMS sender configuration.

 

Step 3: Configure SMS Sender

Please edit this file /config/services.php and add this configuration option.

'nexmo' => [
    'key' => 'NEXMO_KEY',
    'secret' => 'NEXMO_SECRET',
    'sms_from' => 'SENDER_NAME',
],

 

Step 3: Create Notification File

Please go to command prompt or terminal, and navigate to your project folder, and run this command.

php artisan make:notification SMSNotification

The above command will create a class file SMSNotification.php in /demo/app/Notifications.

Note:‚Äč You can use any class name, in here I use SMSNotification.

 

Step 4: Create SMSContent

Please go to edit this file /demo/app/Notifications/SMSNotification.php, and update via() function by changing return value from email to nexmo. Then add this function in the file.

public function toNexmo($notifiable)
    {
        return (new NexmoMessage)
            ->content('Testing Nexmo SMS from Laravel 5');
    }

So the full content of this file should be like this.

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\NexmoMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class SMSNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['nexmo'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', url('/'))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

    public function toNexmo($notifiable)
    {
        return (new NexmoMessage)
            ->content('Testing Nexmo SMS from Laravel 5');
    }
}

 

Step 5: How to Send SMS

To be able to send notification, we have to use Notifiable trait in User model (/demo/app/User.php). 

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
   
    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

 

 Then you can use the code below to send. 

$user = new User();
$user->phone_number= '+85516393555';   // Don't forget specify country code.
$user->notify(new SMSNotification());

Please check laravel website for more detail https://laravel.com/docs/5.5/notifications#sms-notifications.

Note: If you cannot send SMS and see SSL certificate error like below

curl-error-ssl-certificate

Please go to download cacert.pem file from here https://curl.haxx.se/ca/cacert.pem. Then go to php.ini and uncomment option curl.cainfo and change it value to point to cacert.pem file.

[curl]
curl.cainfo = "PATH_TO/cacert.pem"

 

After running the code, you will see an SMS was sent to your phone.

nexmo-sms-laravel

Note: To remove the text at the end of your message, you have to use license account.

Hope you can do it.



Related Posts

© 2018 All Rights Reserved.