简体   繁体   English

当用户验证邮件或密码重置邮件在 Laravel 默认身份验证中触发时,将密件抄送添加到邮件

[英]Add bcc to mail when user verification mail or password reset mail trigger in laravel default Auth

I am trying to add bbc to verification and password rest link mail in laravel default auth system.我正在尝试将 bbc 添加到 Laravel 默认身份验证系统中的验证和密码休息链接邮件中。 There is no mail function in laravel default Auth system how can I add ->bcc() in verification mail and password reset mail. laravel默认Auth系统没有邮件功能,如何在验证邮件和密码重置邮件中添加->bcc()。

Any help would be appriciated.任何帮助将被appriciated。

like this像这样

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

forgotpasswordcontroller.php忘记密码控制器.php

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Password;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
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);
    }
}

verificationcontroller.php验证控制器.php

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Foundation\Auth\VerifiesEmails;

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

then add this code at ResetPassword然后在 ResetPassword 添加此代码

<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;

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 \Illuminate\Notifications\Messages\MailMessage
     */
    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;
    }
}

then override method at user class然后在用户类覆盖方法

<?php

namespace App;

use App\Helper\Helper;
use App\Notifications\ResetPassword;
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 $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));
    }


}

and do same thing to Verification并对验证做同样的事情

Override method for EmailVerification EmailVerification 的覆盖方法

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

You can also listen for the mail sent event, like so:您还可以监听邮件发送事件,如下所示:

app/Providers/EventServiceProvider.php应用程序/Providers/EventServiceProvider.php

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

Then, app/Listeners/LogOutboundMessages.php然后,app/Listeners/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');
    }
}

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

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