简体   繁体   English

Laravel 8:Nexmo 不向手机号码国家/地区巴基斯坦发送代码(无法发送没有发件人地址的消息)

[英]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.我正在使用laravel 8。我想在用户注册时通过nexmo发送通知短信,但它不起作用。 It says Swift_TransportException Cannot send message without a sender address.它说 Swift_TransportException 无法在没有发件人地址的情况下发送消息。 I cannot find the solution for nexmo mobile.我找不到 nexmo mobile 的解决方案。 How to solve this problem in laravel 8.如何在 laravel 8 中解决这个问题。

UserController.php 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用户 Model

 <?php

namespace App;

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

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 MailNotification.php

    <?php

namespace App\Notifications;

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

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 \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 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服务.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',
    ],
];

If you are using the notification channel, you will need to specify a sender address ( docs ).如果您使用通知渠道,则需要指定发件人地址 ( docs )。 In your config/services.yml file you need to add:在您的config/services.yml文件中,您需要添加:

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

where sms_from is a valid sender ID.其中sms_from是有效的发件人 ID。 What constitutes a valid sender ID depends on the country, you can find more information at:有效发件人 ID 的构成取决于国家/地区,您可以在以下位置找到更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM