I am making a console application which uses the SwiftMail extension to send. Due to our policy, I have two virtual machines, one which serves as a SMTP relay and the other which is the application server. Sending mail manually through telnet to the relay works fine. When using SwiftMail, it's broken.
The headers are returned and there are no entries returned in the $failure
variable for send()
Response of getHeaders()->toString()
Message-ID: <[email protected]>
Date: Wed, 24 Oct 2012 14:50:31 -0400
Subject: [YourSite] Feedback
From: [email protected]
To: [email protected]
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
If I echo the send()
, i get 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('[email protected]'))
->setTo(array('[email protected]'))
->setBody("Message!");
$app['mailer']->send($message, $failures);
When i run a TCP dump on the app server and run the script, there are no SMTP connection made, and there are no errors thrown.
Anyone encounter this before? I don't want to use sendmail or mail, but SMTP due to our application requirements.
This is because the SwiftmailerServiceProvider
uses a Swift_MemorySpool
by default, and only flushes that on kernel.terminate
. Let me take a step back and explain each part of this.
The SwiftmailerServiceProvider
is responsible for registering the Swiftmailer services and default configuration. By default the transport (swiftmailer.spooltransport
) is Swift_SpoolTransport
and the swiftmailer.spool
is Swift_MemorySpool
.
Swiftmailer supports different ways of sending the mails. These are called transports. The spool transport acts as a queue. You can either store this queue in a file or in memory. Spool transports have a flushQueue
method which allows flushing the queued mails to a real transport, which should deliver them.
The Symfony2 HttpKernel which Silex uses emits a number of events during the lifecycle of every request. The last one that it emits is the kernel.terminate
event. This event is triggered after the HTTP response body has been sent. This allows you to do heavy tasks after rendering the page, so that it no longer appears as loading to the user.
The SwiftmailerServiceProvider
subscribes to the kernel.terminate
event in order to flush the memory spool after the page has been rendered. It flushes it to swiftmailer.transport
service, which is a Swift_Transport_EsmtpTransport
that does the actual sending via SMTP.
So let's get to the actual problem. You are in a CLI context, so none of those HttpKernel events will be fired. And since the kernel.terminate
event is not fired, your spool is not being flushed. And thus your emails are not getting sent.
There's two good solutions for this:
A) Flush the spool manually. Just do what the provider does in its listener. Add this at the end of your CLI command:
if ($app['mailer.initialized']) {
$app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
}
B) Re-configure the mailer
service to use the ESMTP transport directly without going through the spool:
$app['mailer'] = $app->share(function ($app) {
return new \Swift_Mailer($app['swiftmailer.transport']);
});
Either solution should do. Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With