This tutorial I want to show you how to send sms to mobile phone by using laravel 5 and nexmo.
Go to nexmo website https://www.nexmo.com and register nexmo account. You can register trail account for testing.
Then we will use the key and secret in Laravel SMS sender configuration.
Please edit this file /config/services.php
and add this configuration option.
'nexmo' => [
'key' => 'NEXMO_KEY',
'secret' => 'NEXMO_SECRET',
'sms_from' => 'SENDER_NAME',
],
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.
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');
}
}
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
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.
Note: To remove the text at the end of your message, you have to use license account.
Hope you can do it.
Create ajax CRUD, search, sort and pagination with Laravel 5.5
How to create CRUD, search, sort and pagination with Laravel 5.5