php - Laravel 8:Nexmo 不向手机号码国家巴基斯坦发送代码(没有发件人地址无法发送消息)
问题描述
我正在使用laravel 8。我想在用户注册时通过nexmo发送通知短信,但它不起作用。它说 Swift_TransportException 无法在没有发件人地址的情况下发送消息。我找不到 nexmo mobile 的解决方案。如何在 laravel 8 中解决这个问题。
用户控制器.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;
}
}
邮件通知.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');
}
}
服务.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 的构成取决于国家/地区,您可以在以下位置找到更多信息:
推荐阅读
- c# - How to add emoji to a string then add to .ToList
- ffmpeg - mmpeg 更改生成文件的比特率和文件大小
- angular - Angular 多个模板 - 具有多个组件
- c# - 从 App Id 获取注册的 App 显示名称
- python-3.x - open cv 有助于将图像转换为 numpy 数组的机制
- python - 如何从 df.groupby('feature')['label'].value_counts() 绘制堆积条形图
- python-3.x - PDFInfoNotInstalledError:无法获取页数。poppler 是否已安装并在 PATH 中?
- java - 如何按顺序遍历数组
- java - 如何在小程序中使用 FlowLayout 的 addLayoutComponent 方法?
- wxwidgets - 单击wxWidgets中的按钮时如何绘制一些文本?