Skip to content
Advertisement

Laravel 8: Nexmo not sending code to mobile number country pakistan (Cannot send message without a sender address)

I am using laravel 8. I want to send a notification SMS through nexmo when user register, but it doesn’t work. It says Swift_TransportException Cannot send message without a sender address. I cannot find the solution for nexmo mobile. How to solve this problem in laravel 8.

UserController.php

public function registerUser(Request $request)
{
    if ($request->isMethod('post')) {
        $data = $request->all();
        // echo "<pre>";
        // print_r($data);
        // die;
        //checking if user already register
        $userCount = User::where('email', $data['email'])->count();
        if ($userCount > 0) {
            $message = "Email Already Register";
            session::flash('error_message', $message);
            return redirect()->back();
        } else {
            //Register new user
            $user = new User;
            $user->name = $data['name'];
            $user->mobile = $data['mobile'];
            $user->email = $data['email'];
            $user->password = bcrypt($data['password']);
            $user->status = 1;
            $user->save();

            if (Auth::attempt(['email' => $data['email'], 'password' => $data['password']])) {
                // echo "<pre>";
                // print_r(Auth::user());
                // die;
                //update user cart with user id
                if (!empty(Session::get('session_id'))) {
                    $user_id = Auth::user()->id;
                    $session_id = Session::get('session_id');
                    Cart::where('session_id', $session_id)->update(['user_id' => $user_id]);
                }

                Notification::send($user, new MailNotification);
                return redirect('/');
            }
        }
    }
}

User Model

 <?php

namespace App;

use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function routeNotificationForNexmo($notification)
    {
        return $this->mobile;
    }
}

MailNotification.php

    <?php

namespace AppNotifications;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
use IlluminateNotificationsNotification;
use IlluminateNotificationsMessagesNexmoMessage;

class MailNotification 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 ['mail', 'nexmo'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return IlluminateNotificationsMessagesMailMessage
     */
    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 toNexmo($notifiable)
    {
        return (new NexmoMessage)
            ->content('Dear Customer, You have been registered successfully registered with stylooworld.com. Login to your account to access orders and available offers');
    }
}

service.yml

    <?php

return [

    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
    ],

    'postmark' => [
        'token' => env('POSTMARK_TOKEN'),
    ],

    'ses' => [
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
    ],

    'nexmo' => [

        'sms_from' => '03025496045',
    ],
];

Advertisement

Answer

If you are using the notification channel, you will need to specify a sender address (docs). In your config/services.yml file you need to add:

'nexmo' => [
    'sms_from' => '15556666666',
],

where sms_from is a valid sender ID. What constitutes a valid sender ID depends on the country, you can find more information at:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement