[英]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.