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

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值