问题 Laravel 5 SwiftMailer发送:如何获取错误代码


美好的一天,

我发送的电子邮件类似于: Laravel 5:发送电子邮件。我是这样做的:

$mail_status = Mail::send( 'emails.followup', $data, function( $message ) use ($data) {
             $message->to( $data['email'] )
            ->from( $data['email_from'], $data['email_name'] )
            ->subject( $data['subject'] );
        });

if($mail_status) {
    $ret_status = 'sent';
}
else
    $ret_status = 'sent_failed';

现在,如果 $ret_status 是'sent_failed',我想知道发生了什么。我该怎么做?如何从邮件服务器查看邮件?

这是我的 .env 文件:

MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.com
MAIL_PORT=465
MAIL_USERNAME=noreply@mydomain.com
MAIL_PASSWORD=thepassword
MAIL_ENCRYPTION=ssl

更新:

看起来上面的方法是Laravel 4.如果您知道如何使用Laravel 5+获取错误代码,我可以将其视为正确的答案。


5845
2017-08-22 13:50


起源



答案:


您可以使用try catch,也可以误认为使用 $email, $email_from, $email_name, $subject 没有进入功能范围。

try{
    $mail_status = Mail::send( 'emails.followup', $data, function( $message ) use ($data, $email, $email_from, $email_name, $subject) {
                        $message->to( $email )
                        ->from( $email_from, $email_name )
                        ->subject( $subject );
                    });
    //If error from Mail::send
    if($mail_status->failures() > 0){
        //Fail for which email address...
        foreach(Mail::failures as $address) {
            print $address . ', ';
        }
        exit;
    }  
}
catch(\Exception $e){
    // Get error here
    print $e->getMessage();
    exit;
}

添加了电子邮件地址的故障打印,以检查电子邮件失败的地址。


6
2017-08-22 14:06



@ user1506104:检查这个答案,希望你能从中得到解决方案 - AddWeb Solution Pvt Ltd
你好。这没用。还有其他建议吗? @AddWeb - user1506104
是什么 print $e->getMessage(); 为你? - AddWeb Solution Pvt Ltd
没有例外。 $ mail_status虽然是0(假)。这意味着,没有发送任何东西。正确? - user1506104
现在添加故障案例检查 - AddWeb Solution Pvt Ltd


答案:


您可以使用try catch,也可以误认为使用 $email, $email_from, $email_name, $subject 没有进入功能范围。

try{
    $mail_status = Mail::send( 'emails.followup', $data, function( $message ) use ($data, $email, $email_from, $email_name, $subject) {
                        $message->to( $email )
                        ->from( $email_from, $email_name )
                        ->subject( $subject );
                    });
    //If error from Mail::send
    if($mail_status->failures() > 0){
        //Fail for which email address...
        foreach(Mail::failures as $address) {
            print $address . ', ';
        }
        exit;
    }  
}
catch(\Exception $e){
    // Get error here
    print $e->getMessage();
    exit;
}

添加了电子邮件地址的故障打印,以检查电子邮件失败的地址。


6
2017-08-22 14:06



@ user1506104:检查这个答案,希望你能从中得到解决方案 - AddWeb Solution Pvt Ltd
你好。这没用。还有其他建议吗? @AddWeb - user1506104
是什么 print $e->getMessage(); 为你? - AddWeb Solution Pvt Ltd
没有例外。 $ mail_status虽然是0(假)。这意味着,没有发送任何东西。正确? - user1506104
现在添加故障案例检查 - AddWeb Solution Pvt Ltd


检查 Mail::failures() 之后立马 Mail::send() 将返回您尝试向其发送电子邮件的失败电子邮件地址数组。

但是,据我所知,使用Mail facade失败时无法获得确切的错误。为此,您需要在您的上面设置debug true .env 并查看Laravel日志。


5
2017-08-27 17:29



日志也没有给我任何东西。谢谢你的建议。 +1 - user1506104


  1. 要获取dev版本的错误(在屏幕上),请在.env文件中设置 APP_DEBUG=true
  2. 要获得确切的消息,请执行try catch,但要抓住 Swift_TransportException  https://stackoverflow.com/a/42221501/2119863
  3. 如果需要,您还可以通过以下方式获取邮件程序实例 app('mailer') 并在那里做一些研究。

这是一个有效的例子:

try{
    $mail_status = Mail::send( 'emails.followup', $data, function( $message ) use ($data) {
        $message->to( $data['email'] )
        ->from( $data['email_from'], $data['email_name'] )
        ->subject( $data['subject'] );
    });
}catch(\Swift_TransportException $e){
    dd($e, app('mailer'));
}

dd() 结果:

Swift_TransportException {#237 ▼
  #message: """
    Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required\r\n
    "
    """
  #code: 530
  #file: ...

3
2017-08-31 09:59