-->

Silex的SwiftMailer不使SMTP连接在执行时(Silex SwiftMailer No

2019-08-01 21:37发布

我在做它使用SwiftMail扩展发送控制台应用程序。 由于我们的政策,我有两个虚拟机,其中一个充当SMTP中继,另一个是应用服务器。 通过telnet手动发送邮件到继电器工作正常。 当使用SwiftMail,它的分解。

标题是返回,也没有在返回的条目$failure变量send()

的响应getHeaders()->toString()

Message-ID: <1351104631.508838778dc03@swift.generated>
Date: Wed, 24 Oct 2012 14:50:31 -0400
Subject: [YourSite] Feedback
From: noreply@localhost.com
To: sixeightzero@localhost.com
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

如果我呼应send()我得到1

boot.php

$app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
    'swiftmailer.options' => array(
        'host' => 'ip.host.relay',
        'port' => 25,
        'encryption' => null,
        'auth_mode' => null
    ),
));

app.php

 $message = \Swift_Message::newInstance( )
        ->setSubject('[YourSite] Feedback')
        ->setFrom(array('noreply@localhost.com'))
        ->setTo(array('sixeightzero@localhost.com'))
        ->setBody("Message!");


    $app['mailer']->send($message, $failures);

当我运行的应用程序服务器上的TCP转储和运行脚本,有没有做SMTP连接,并且没有抛出错误。

任何人之前遇到这样的? 我不想使用sendmail或邮件,但SMTP由于我们的应用需求。

Answer 1:

这是因为SwiftmailerServiceProvider使用Swift_MemorySpool默认情况下,只有刷新上kernel.terminate 。 让我退后一步并解释这每一个部分。

  • SwiftmailerServiceProvider是负责注册Swiftmailer服务和默认配置。 默认情况下,运输( swiftmailer.spooltransport )是Swift_SpoolTransportswiftmailer.spoolSwift_MemorySpool

  • Swiftmailer支持发送邮件的方式不同。 这些都是所谓的运输。 线轴输送充当队列。 您可以此队列存储在一个文件或内存中。 后台传输有flushQueue方法,它允许冲洗排队邮件到一个真实的运输,这应该救他们。

  • 其中捷希凯使用Symfony2的HttpKernel每个请求的生命周期期间发射多个事件。 它发出的最后一个是kernel.terminate事件。 HTTP响应体发送后触发此事件。 这可以让你在渲染页面后做繁重的任务,所以它不再表现为加载到用户。

  • SwiftmailerServiceProvider预订了kernel.terminate事件以刷新内存阀芯的页面已经呈现后。 它刷新它swiftmailer.transport服务,这是一个Swift_Transport_EsmtpTransport执行实际发送通过SMTP。

所以,让我们实际的问题。 你是在一个CLI背景下,所以没有那些HttpKernel事件将被解雇。 而且,由于kernel.terminate将不会触发事件,你的阀芯没有被刷新。 因而你的电子邮件没有得到发送。

有这两个很好的解决方案:

  • A)手动冲洗卷轴上。 只是做了提供者在其监听什么。 在您的CLI命令的末尾添加这样的:

     if ($app['mailer.initialized']) { $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']); } 
  • B)重新配置mailer服务使用ESMTP传输的情况下直接通过滑去:

     $app['mailer'] = $app->share(function ($app) { return new \Swift_Mailer($app['swiftmailer.transport']); }); 

两种解决方案应该做的。 祝好运!



文章来源: Silex SwiftMailer Not Making SMTP Connection Upon Execution