Yii2版本:2.0.3
SwiftMail版本:v5.3.1
使用定时任务发邮件时,由于swiftmailer长时间运行没有关闭transport,造成SSL连接出错。
报错1:
exception 'Swift_TransportException' with message 'Expected response code 250 but got code "", with message ""' invendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:383
报错2:
exception 'yii\base\ErrorException' with message 'fwrite(): SSL: Broken pipe' invendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:232
解决方案:
1.修改vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php:90
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$failedRecipients = (array) $failedRecipients;
if (!$this->_transport->isStarted()) {
$this->_transport->start();
}
$sent = 0;
try {
$sent = $this->_transport->send($message, $failedRecipients);
} catch (Swift_RfcComplianceException $e) {
foreach ($message->getTo() as $address => $name) {
$failedRecipients[] = $address;
}
} catch (\Exception $exception) {
$this->_transport->stop();
sleep(10);
throw $exception;
} finally {
$this->_transport->stop();
sleep(1);
}
return $sent;
}
2.在调用发送邮件处进行失败重试
/**
* @return bool
*/
private function sendEmail()
{
$tmpPath = \Yii::$app->getRuntimePath() . '/logs/';
$tmpFilePath = File::downFileForNotExist($tmpPath, $this->filePath);
$result = \Yii::$app->mailer->compose()
->setTo($this->emailTo)
->setFrom(['nick@lampnick.com' => 'lampNick'])
->setSubject($this->subject)
->setTextBody($this->body)
->attach($tmpFilePath)
->send();
@unlink($tmpFilePath);
return $result;
}
/**
* 尝试发邮件
*/
private function trySendMail()
{
for ($i = 1; $i <= self::TRY_COUNT; $i++) {
try {
$sendResult = $this->sendEmail();
if (!$sendResult) {
throw new SendElectronicDocumentMailAsynException;
}
return;
} catch (\Exception $e) {
\Yii::error("swiftMailer发邮件失败,重试第" . $i . "次,数据为:" .
json_encode($this->messageInfo) . "失败原因是:" . (string)$e);
\Yii::getLogger()->flush(true);
}
}
\Yii::error("swiftMailer发邮件最终失败,数据为:" . json_encode($this->messageInfo));
\Yii::getLogger()->flush(true);
}
喜欢 (2)or分享 (0)