在laravel默认Auth中触发用户验证邮件或密码重置邮件时,将bcc添加到邮件中



我正在尝试将bbc添加到laravel默认身份验证系统中的验证和密码休息链接邮件中。在laravel默认Auth系统中没有邮件功能,我如何添加->验证邮件和密码重置邮件中的bcc((。

任何帮助都会得到通知。

像这个

Mail::to($request->user())
->cc($moreUsers)
->bcc('admin@example.com')
->send(new OrderShipped($order));

forgetpasswordcontroller.php

<?php
namespace AppHttpControllersAuth;
use IlluminateHttpRequest;
use AppHttpControllersController;
use IlluminateSupportFacadesPassword;
use IlluminateFoundationAuthSendsPasswordResetEmails;
use Auth;
class ForgotPasswordController extends Controller
{          
use SendsPasswordResetEmails;

public function __construct()
{
$this->middleware('guest');
}
public function sendResetLinkEmail(Request $request)
{
$this->validateEmail($request);

$response = $this->broker()->sendResetLink(
$request->only('email')
);

return $response == Password::RESET_LINK_SENT
? $this->sendResetLinkResponse($request, $response)
: $this->sendResetLinkFailedResponse($request, $response);
}
}

验证控制器.php

<?php
namespace AppHttpControllersAuth;
use IlluminateHttpRequest;
use IlluminateRoutingController;
use IlluminateFoundationAuthVerifiesEmails;
class VerificationController extends Controller
{       
use VerifiesEmails;

protected $redirectTo = 'shop/home';

public function __construct()
{
$this->middleware('auth')->only('verify');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
}
php artisan make:notification ResetPassword

然后在ResetPassword 中添加此代码

<?php
namespace AppNotifications;
use IlluminateNotificationsMessagesMailMessage;
use IlluminateNotificationsNotification;
use IlluminateSupportFacadesLang;
class ResetPassword extends Notification
{
/**
* The password reset token.
*
* @var string
*/
public $token;
/**
* The callback that should be used to build the mail message.
*
* @var Closure|null
*/
public static $toMailCallback;
/**
* Create a notification instance.
*
* @param  string  $token
* @return void
*/
public function __construct($token)
{
$this->token = $token;
}
/**
* Get the notification's channels.
*
* @param  mixed  $notifiable
* @return array|string
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Build the mail representation of the notification.
*
* @param  mixed  $notifiable
* @return IlluminateNotificationsMessagesMailMessage
*/
public function toMail($notifiable)
{
if (static::$toMailCallback) {
return call_user_func(static::$toMailCallback, $notifiable, $this->token);
}
return (new MailMessage)        
->subject(Lang::get('Reset Password Notification'))
->bcc('info@example.com') //add bcc for another email
->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
->action(Lang::get('Reset Password'), url(route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')]))
->line(Lang::get('If you did not request a password reset, no further action is required.'));
}
/**
* Set a callback that should be used when building the notification mail message.
*
* @param  Closure  $callback
* @return void
*/
public static function toMailUsing($callback)
{
static::$toMailCallback = $callback;
}
}

然后覆盖用户类的方法

<?php
namespace App;
use AppHelperHelper;
use AppNotificationsResetPassword;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table = 'users';
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
// Override sendPasswordResetNotification
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token));
}

}

并对验证做同样的事情

EmailVerification 的覆盖方法

public function sendEmailVerificationNotification()
{
$this->notify(new VerifyEmail);
}

您还可以监听邮件发送事件,如下所示:

app/Providers/EventServiceProvider.php

/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
MessageSending::class => [
LogOutboundMessages::class
]
];

然后,app/Listers/LogOutboundMessages.php

class LogOutboundMessages
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param  object  $event
* @return void
*/
public function handle(MessageSending $event)
{
$event->message->addBcc('admin@example.com');
}
}

最新更新