簡體   English   中英

Silex SwiftMailer在執行時不進行SMTP連接

[英]Silex SwiftMailer Not Making SMTP Connection Upon Execution

我正在制作一個使用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。

這是因為SwiftmailerServiceProvider默認使用Swift_MemorySpool ,只刷新kernel.terminate上的kernel.terminate 讓我退后一步,解釋一下這一部分。

  • SwiftmailerServiceProvider負責注冊Swiftmailer服務和默認配置。 默認情況下,transport( swiftmailer.spooltransport )是Swift_SpoolTransportswiftmailer.spoolSwift_MemorySpool

  • Swiftmailer支持不同的郵件發送方式。 這些被稱為運輸。 假脫機傳輸充當隊列。 您可以將此隊列存儲在文件或內存中。 假脫機傳輸有一個flushQueue方法,允許將排隊的郵件刷新到真正的傳輸,這應該傳遞它們。

  • Silex使用的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']); }); 

兩種解決方案都應該做到。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM