Laravel以超级管理员的身份从管理员控制器发送通知
我有超级管理员和管理员角色。在管理员视图中,我添加了请求验证电子邮件的按钮。我遇到的问题是,当管理员单击按钮接收验证电子邮件时,电子邮件来自管理员,而不是超级管理员 如何使它从超级管理员发送到管理员,而不是从管理员 路线:Laravel以超级管理员的身份从管理员控制器发送通知,laravel,Laravel,我有超级管理员和管理员角色。在管理员视图中,我添加了请求验证电子邮件的按钮。我遇到的问题是,当管理员单击按钮接收验证电子邮件时,电子邮件来自管理员,而不是超级管理员 如何使它从超级管理员发送到管理员,而不是从管理员 路线: Route::post('/dashboard/SendEmailVerification', 'AdminDashboardController@SendEmailVerification')->name('dashboard.SendEmailVerificatio
Route::post('/dashboard/SendEmailVerification', 'AdminDashboardController@SendEmailVerification')->name('dashboard.SendEmailVerification');
在AdminDashboardController中:
use App\Notifications\EmailVerification;
use App\User;
.............
public function SendEmailVerification(Request $request){
$user = User::where('email_verification_code', $request->code)
->withoutGlobalScope('active')
->first();
$user->notify(new EmailVerification($user));
return Reply::success('Email sent!');
}
以及通知电子邮件:
namespace App\Notifications;
use App\Traits\SmtpSettings;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\User;
class EmailVerification extends Notification implements ShouldQueue
{
use Queueable, SmtpSettings;
protected $user;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
$this->setMailConfigs();
}
/**
* Get the notification's delivery channels.
*t('mail::layout')
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
$via = ['mail'];
return $via;
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Confirm your email')
->greeting(__('Hello!'))
->line(__('email.emailVerify.text'))
->action('Confirm', getDomainSpecificUrl(route('front.get-email-verification', $this->user->email_verification_code), $this->user->company));
#->line(__('email.thankyouNote'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return $notifiable->toArray();
}
}
您收到的发件人地址错误,因为您从未在通知配置中定义发件人。有两种方法可以做到这一点: 第一个:第一个是一个非常简单但非动态的解决方案。在.env配置中,添加以下行:
MAIL_FROM_NAME="My Name"
MAIL_FROM_ADDRESS=support@example.com
配置时,将其添加到yout config/mail.php配置中:
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'default value if not found in .env'),
'name' => env('MAIL_FROM_NAME', 'default value if not found in .env'),
],
注意:执行此操作时,不要忘记清除缓存,并重新启动队列:
Second:这是一个更加动态的解决方案,因为您可以从数据库加载发件人电子邮件地址。在sendmailverification
方法中,您可以查询超级管理员用户,并传递到EmailVerification
类:
public function SendEmailVerification(Request $request){
$user = User::where('email_verification_code', $request->code)
->withoutGlobalScope('active')
->first();
$superAdminUser = User::where('role', 'super-admin')->first();
$user->notify(new EmailVerification($user, $superAdminUser));
return Reply::success('Email sent!');
}
然后,更改EmailVerification类:
public $user;
public $superUser;
public function __construct(User $user, User $superUser)
{
$this->user = $user;
$this->superUser = $superUser;
$this->setMailConfigs();
}
在您的toMail()
方法中,添加另一个from()
方法:
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Confirm your email')
->from($superAdminUser->email, $superAdminUser->first_name)
->greeting(__('Hello!'))
->line(__('email.emailVerify.text'))
->action('Confirm', getDomainSpecificUrl(route('front.get-email-verification', $this->user->email_verification_code), $this->user->company));
#->line(__('email.thankyouNote'));
}
注意:还要清除缓存并重新启动队列
希望这能解决你的问题。如果您对这些解决方案有任何问题,请告诉我
您可以在上阅读更多关于通知的信息。您所说的“如何使此信息从超级管理员发送到管理员,而不是从管理员发送到管理员”的确切含义是什么。您的案例中有什么错误,错误的发件人电子邮件,错误的电子邮件模板中的名称,或者其他什么?错误的发件人电子邮件和名称。电子邮件来自管理员用户,而不是超级管理员用户。