Laravel - 如何检查重置密码电子邮件成功发送给用户?

时间:2015-12-31 05:27:37

标签: php laravel laravel-5 laravel-5.1

我在postEmail中有以下功能PasswordController.php,并在用户尝试重设密码时进行调用。

/**
     * Send a reset link to the given user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postEmail(Request $request)
    {
        //echo Input::get('ID'); die;
        $this->validate($request, ['ID' => 'required|email']);

        // Pass data to reset password mail template
        view()->composer('emails.password', function($view) {
            $view->with([
                'UserProduct1'   => 'UserProduct1',
                'UserProduct2'   => 'UserProduct2',
            ]);
        });

        $response = Password::sendResetLink($request->only('ID'), function (Message $message) {
            $message->subject($this->getEmailSubject());
        });

        switch ($response) {
            case Password::RESET_LINK_SENT:
                return redirect()->back()->with('status', trans($response));

            case Password::INVALID_USER:
                return redirect()->back()->withErrors(['ID' => trans($response)]);
        }
    }

有没有办法在laravel中检查,重置是否发送给用户的电子邮件。

任何想法?

感谢。

1 个答案:

答案 0 :(得分:0)

如果您使用的是Laravel 5.1 Mailer Class,则很容易检测到邮件是否已发送

  

<强> Sending Mail

<?php

namespace App\Http\Controllers;

use Mail;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Send an e-mail reminder to the user.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function sendEmailReminder(Request $request, $id)
    {
        $user = User::findOrFail($id);

        $mailSend = Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
            $m->from('hello@app.com', 'Your Application');

            $m->to($user->email, $user->name)->subject('Your Reminder!');
        });//Here it returns 0 if email not send

        if ($mailSend === 0)
        {
            //Your Code
        }
    }
}

当您拨打 Mail::send 时,会调用此方法https://github.com/laravel/framework/blob/5.1/src/Illuminate/Mail/Mailer.php#L157然后会调用 sendSwiftMessage https://github.com/laravel/framework/blob/5.1/src/Illuminate/Mail/Mailer.php#L388 。然后它会调用swiftmailer的 send 方法https://github.com/swiftmailer/swiftmailer/blob/5.x/lib/classes/Swift/Mailer.php#L74&amp;正如你所看到的那样,当它失败时它会返回$ sent变量,如果失败则为0。

更多https://laravel.com/docs/5.1/mail#sending-mail