一旦工作,那就要努力的干,聪明的干,快速的干——用省下来的时间干自己喜欢干的事情。!

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

php lampnick 4159℃ 0评论

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@mitnick.fun' => '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);
}

转载请注明:MitNick » Yii2-swiftmailer发邮件失败报fwrite(): SSL: Broken pipe

喜欢 (2)or分享 (0)
头像
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址