gpt4 book ai didi

php - 如何在 zend 框架 2 中邮件传输失败时返回 false?

转载 作者:行者123 更新时间:2023-12-04 18:05:16 24 4
gpt4 key购买 nike

如果邮件发送失败,我试图用 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;
}

这是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.');
}
},
});

但在失败的情况下,由于不正确的身份验证数据,我得到了内部服务器错误,因此不会显示失败消息

最佳答案

您只捕获了 Zend\Mail\Transport\Exception\DomainException,但 SMTP 传输适配器也可以抛出其他异常。例如在 send它可以抛出 Zend\Mail\Transport\Exception\RuntimeException 的方法。

我建议捕获邮件包的异常接口(interface):

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

关于php - 如何在 zend 框架 2 中邮件传输失败时返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28217771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com