我的服务器连接看起来不稳定,因为有时它成功发送电子邮件,有时它会失败。
错误说
Swift_TransportException
Connection to ssl://in-v3.mailjet.com:465 Timed Out
在上述条件下,我尝试更改代码以在捕获异常时重新发送电子邮件。
这是我在控制器中的代码。
//this line after import class
ini_set('memory_limit', '256M');
public function resend_on_error($tried)
{
try{
$message = Yii::$app->mail->compose();
if (Yii::$app->user->isGuest) {
$message->setFrom('from@domain.com');
} else {
$message->setFrom(Yii::$app->user->identity->email);
}
$message->setTo(Yii::$app->params['adminEmail'])
->setFrom(Yii::$app->params['adminEmail'])
->setTo("mymail@gmail.com")
->setSubject('Reset Password '.$tried)
->setHtmlBody($this->renderAjax('//email/_konten',['content'=>'goes here']))
->send();
return 1;
}catch(Swift_TransportException $e){
$this->resend_on_error($tried++);
}
}
public function actionEmail()
{
$tried = 1;
if($this->resend_on_error($tried) == 1){
return "send success";
}
}
但我得到了这个
Allowed memory size of 268435456 bytes exhausted (tried to allocate 4096 bytes)
请告诉我我在这里做错了什么?
提前谢谢。
这不是
很明显吗?您正在捕获 SSL 超时异常,但您立即再次运行相同的函数并抛出相同的异常,此循环会发生,直到内存耗尽。
您确实递增$tried
,但不检查其值。我认为您应该尝试发送邮件 5 次,如果它仍然不起作用,则必须严重损坏某些东西并且需要技术关注,在这种情况下,请捕获 SwiftTransport 异常并将另一个扔给最终用户。