发送Laravel电子邮件时显示自定义错误

时间:2018-08-04 06:22:39

标签: php laravel email laravel-5.6 laravel-mail

当第二个发送函数中提供的电子邮件正确$ cm1 ['email']时,下面的代码可以完美地工作,但是,当用户输入不起作用的电子邮件时,会显示Laravel错误页面,并且我写了redirect( '/ cm'),并显示错误消息。因此,我的问题是,当发送电子邮件而不是显示错误时发生错误时,如何使Laravel将用户重定向到带有错误Flash消息的页面。

     Mail::send('email_contact', ['data' => $cm1], function ($message)
    {

        $message->from('noreply@mydomain.com', 'No Reply');
        $message->to('rec@gmail.com');

    });

    Mail::send('email_rec', ['data' => $cm1],function ($message) use ($cm1) {

        $message->from('noreply@mydomain.com', 'No Reply');
        $message->to( $cm1['email']);

    });
    if( count(Mail::failures()) > 0 ) {
        return redirect('/cm')->with('flash_message','<strong> Something went wrong! </strong> The entered email seems to be invalid Received');
    }

    return redirect('/cm')->with('flash_message','<strong> Thank You! </strong> Your Inquiry Has Been Received');

1 个答案:

答案 0 :(得分:0)

您可以在app / Exceptions / Handler.php文件中进行如下更改。 也许这可以解决您的问题。

public function render($request, Exception $e)
{

    if ($this->isHttpException($e)) {
        switch ($e->getStatusCode()) {

            // not authorized
            case '403':
                return \Response::view('errors.'.$e->getStatusCode(), [],403);
                break;

            // not found
            case '404':
                return \Response::view('errors.403', [],404);
                break;

            // not found
            case '405':
                return \Response::view('errors.403', [],405);
                break;

            // internal error
            case '500':
                return \Response::view('errors.403',[],500);
                break;

            default:
                return $this->renderHttpException($e);
                break;
        }
    }elseif ($request->ajax()) {
        return response()->json(['message' => "Something want wrong.!!"]);
    }elseif ($e instanceof QueryException){
        return back();
    }elseif ($e instanceof PDOException){
        return back();
    }
    else {
        return parent::render($request, $e);
    }
}

在视图中,您可以将视图文件设置为呈现