Laravel 8:Nexmo 未向手机号码国家/地区巴基斯坦发送代码(无法在没有发件人地址的情况下发送消息)
Laravel 8: Nexmo not sending code to mobile number country pakistan (Cannot send message without a sender address)
我正在使用 laravel 8。我想在用户注册时通过 nexmo 发送通知短信,但它不起作用。它说 Swift_TransportException 没有发件人地址无法发送邮件。我找不到 nexmo mobile 的解决方案。如何在 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('/');
}
}
}
}
用户模型
<?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
<?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
<?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',
],
];
如果您使用的是通知渠道,则需要指定发件人地址 (docs)。在您的 config/services.yml
文件中,您需要添加:
'nexmo' => [
'sms_from' => '15556666666',
],
其中 sms_from
是有效的发件人 ID。什么构成有效的发件人 ID 取决于国家/地区,您可以在以下位置找到更多信息:
我正在使用 laravel 8。我想在用户注册时通过 nexmo 发送通知短信,但它不起作用。它说 Swift_TransportException 没有发件人地址无法发送邮件。我找不到 nexmo mobile 的解决方案。如何在 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('/');
}
}
}
}
用户模型
<?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
<?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
<?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',
],
];
如果您使用的是通知渠道,则需要指定发件人地址 (docs)。在您的 config/services.yml
文件中,您需要添加:
'nexmo' => [
'sms_from' => '15556666666',
],
其中 sms_from
是有效的发件人 ID。什么构成有效的发件人 ID 取决于国家/地区,您可以在以下位置找到更多信息: