Laravel 5 - 如何捕获 Mail::send() 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41513882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:36:12  来源:igfitidea点击:

Laravel 5 - How Do You Catch an Mail::send() Error?

laravellaravel-5laravel-mail

提问by Lloyd Banks

I have the following method which sends out an e-mail:

我有以下发送电子邮件的方法:

Mail::send('emails.configuration_test', array(), function($email)use($request){
    $email->to($request->test_address)->subject('Configuration Test');
});

If the above errors out, I'd like to be able to catch the exception. When I use the following:

如果出现上述错误,我希望能够捕获异常。当我使用以下内容时:

try{
    Mail::send('emails.configuration_test', array(), function($email)use($request){
        $email->to($request->test_address)->subject('Configuration Test');
    });
}
catch(Exception $e){
    // Never reached
}

the exception is never caught. Instead I get a Laravel stacktrace as the response if the send()method errors out.

异常永远不会被捕获。相反,如果send()方法出错,我会得到一个 Laravel 堆栈跟踪作为响应。

How do I catch the exception in this case?

在这种情况下如何捕获异常?

回答by Lloyd Banks

Using the root namespace \Exceptiondid the trick.

使用根命名空间可以解决问题\Exception

Instead of:

代替:

catch(Exception $e){
    // Never reached
}

I used:

我用了:

catch(\Exception $e){
    // Get error here
}