当前位置: 首页 > 工具软件 > SwiftMailer > 使用案例 >

Yii2-swiftmailer发邮件失败报fwrite(): SSL: Broken pipe

魏翔
2023-12-01

个人博客原文地址:http://www.lampnick.com/php/706

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 ""' in 
vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:383

报错2:

exception 'yii\base\ErrorException' with message 'fwrite(): SSL: Broken pipe' in  
vendor/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()
{
    $tryCount = self::TRY_COUNT;
    while ($tryCount > 0) {
        try {
            $sendResult = $this->sendEmail();
            if (!$sendResult) {
                throw new SendMailAsynException;
            }
            return;
        } catch (\Exception $e) {
            $tryCount--;
            \Yii::error("swiftMailer发邮件失败,重试发送,失败原因是:" . (string)$e);
            \Yii::getLogger()->flush(true);
            $this->trySendMail();
        }
    }
}
 类似资料: