简体   繁体   English

当zend Framework 2中的邮件传输失败时,如何返回false?

[英]how to return false when mail transport failed in zend framework 2?

I am trying to return fail message with json if the mail sending is failed, here is my code: 如果邮件发送失败,我试图用json返回fail消息,这是我的代码:

$transport = new SmtpTransport();
            $options   = new SmtpOptions(array(
                    'name'              => 'localhost',
                    'host'              => 'localhost',
                    'connection_class'  => 'login',
                    'connection_config' => array(
                            'username' => 'cscsacsac',
                            'password' => 'csdca',
                            //'ssl'=>'ssl',
                            //'port' => '465',
                    ),
            ));

            $transport->setOptions($options);
            $sent = true;
            try {

                $transport->send($mail);
            }
            catch (\Zend\Mail\Transport\Exception\DomainException $e) {
                $sent = false;
            }

            if($sent){

                echo "success";

                exit;
            }

            elseif(!$sent){

                echo "fail";

                exit;
            }

and here is the ajax: 这是ajax:

$.ajax({
          type: "POST",
          url: '<?php echo MAIN_URL?>cases',
          data: { name: name, email: email, mobile: mobile, address: address, compliant: compliant },
          success:function(data){

            if(data =='success'){

                $('.casesMailResponse').css({'color':'#00917D'});
                $('.casesMailResponse').html('Your case has been sent successfully.');
            }

            else if(data == 'fail'){

                $('.casesMailResponse').css({'color':'#F6565B'});
                $('.casesMailResponse').html('Some errors occurred, please try again.');
            }
          },
        });

but in case of failure I got Internal server error due to Incorrect authentication data , so the failure message is not displayed 但是如果失败,由于Incorrect authentication data ,我会收到内部服务器错误消息,因此不会显示失败消息

You are catching only Zend\\Mail\\Transport\\Exception\\DomainException , but SMTP transport adapter can throw other exceptions too. 您仅捕获Zend\\Mail\\Transport\\Exception\\DomainException ,但是SMTP传输适配器也可能引发其他异常。 For example in send method it can throw Zend\\Mail\\Transport\\Exception\\RuntimeException . 例如,在send方法中,它可以引发Zend\\Mail\\Transport\\Exception\\RuntimeException

I suggest to catch exception interface of mail package: 我建议捕获邮件包的异常接口:

    $sent = true;
    try {
        $transport->send($mail);
    } catch (\Zend\Mail\Transport\Exception\ExceptionInterface $e) {
        $sent = false;
    }

Could you try : 您可以尝试:

 }
 catch (\Zend\Mail\Transport\Exception\DomainException $e) {
    $sent = false;
 } catch (Exception $e) {
    $sent = false;
}

Just a guess.... 只是一个猜测...。

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

相关问题 Zend Framework:尝试使用Zend Mail Transport发送带附件的多封电子邮件时出现致命错误 - Zend Framework: Fatal error when trying to use Zend Mail Transport to send multiple emails with attachments Zend Framework-isValid返回false,但getMessages()为空 - Zend Framework - isValid return false, but getMessages() is empty 安装Zend Mail for Zend框架时,mail.php在哪里? - Where is mail.php when installing the Zend Framework for Zend Mail? 在Zend Framework 2.4中为Zend \\ Mail \\ Message设置默认的传输方法 - Setting up default transport method in Zend Framework 2.4 for Zend\Mail\Message 如何修复“无法在Zend Mail中打开套接字(Zend Framework 2) - How to fix "could not open socket in Zend Mail (Zend Framework 2) zf2:如何将zend邮件传输配置存储在mail.local.php文件中? - zf2: How can I store my zend mail transport config in a mail.local.php file? Zend Framework发送邮件 - Zend Framework Send Mail 在Zend中找不到类&#39;\\ Zend_Mail_Transport_Smtp&#39; - Class '\Zend_Mail_Transport_Smtp' not found in Zend 如何在Zend_Mail_Transport_Smtp适配器中使用Amazon SES密钥,机密+ smtp地址? - How to use Amazon SES key, secret + smtp address in my Zend_Mail_Transport_Smtp adapter? 如何按页面返回结果集zend Framework 2 - How to Return resultset by pages zend framework 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM