Skip to content

Instantly share code, notes, and snippets.

@johnwards
Created August 4, 2011 14:15
Show Gist options
  • Save johnwards/1125244 to your computer and use it in GitHub Desktop.
Save johnwards/1125244 to your computer and use it in GitHub Desktop.
Sending emails in Silex via Swiftmailer spool
<?php
//...your silex bootstrap.
//Configure Swiftmail to use SMTP.
$app->register(new SwiftmailerExtension(), array(
'swiftmailer.options' => array(
'host' => 'smtp.gmail.com',
'port' => 465,
'username' => 'silex.swiftmailer@gmail.com',
'password' => 'simplepassword',
'encryption' => 'ssl',
'auth_mode' => 'login'),
'swiftmailer.class_path' => __DIR__.'/../vendor/swiftmailer/lib/classes'
));
//Right we now need to set the transport to Spool.
//Set the directory you want the spool to be in.
$app["swiftmailer.spool"] = new \Swift_FileSpool(__DIR__."/../spool");
//Take a copy of the original transport, we'll need that later.
$app["swiftmailer.transport.original"] = $app["swiftmailer.transport"];
//Create a spool transport
$app["swiftmailer.transport"] = new \Swift_Transport_SpoolTransport($app['swiftmailer.transport.eventdispatcher'], $app["swiftmailer.spool"]);
//...more silex boostrap
#!/usr/bin/env php
<?php
//Bootstrap our Silex application
$app = require __DIR__ . '/../src/bootstrap.php';
//Include the namespaces of the components we plan to use
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
//Instantiate our Console application
$console = new Application('Silex Console', '1');
$console->register( 'send-email' )
->setDefinition( array(
new InputOption('message-limit', '', InputOption::VALUE_OPTIONAL, 'The maximum number of messages to send.'),
new InputOption('time-limit', 0, InputOption::VALUE_OPTIONAL, 'The time limit for sending messages (in seconds).'),
)
)
->setDescription('Send emails from the spool')
->setHelp(<<<EOF
The <info>swiftmailer:spool:send</info> command sends all emails from the spool.
<info>php bin/console.php send-email --message-limit=10 --time-limit=10</info>
EOF
)
->setCode(
function (InputInterface $input, OutputInterface $output) use ($app) {
$transport = $app["swiftmailer.transport"];
if ($transport instanceof \Swift_Transport_SpoolTransport) {
$spool = $transport->getSpool();
$spool->setMessageLimit($input->getOption('message-limit'));
$spool->setTimeLimit($input->getOption('time-limit'));
$sent = $spool->flushQueue($app["swiftmailer.transport.original"]);
$output->writeln(sprintf('sent %s emails', $sent));
}
}
);
$console->run();
@lwojciechowski
Copy link

Registering swiftmailer changed lately a little bit. If someone would like to use it here's the correct code for boostrap :)

$app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
'swiftmailer.options' => array(
'host' => 'smtp.gmail.com',
'port' => 465,
'username' => 'silex.swiftmailer@gmail.com',
'password' => 'simplepassword',
'encryption' => 'ssl',
'auth_mode' => 'login')
));

@meetmatt
Copy link

Thanks!

@devnix
Copy link

devnix commented Jun 3, 2016

Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "xxx" using 1 possible authenticators'

@feedmeastraycat
Copy link

feedmeastraycat commented Jan 4, 2017

This doesn't work with Silex 2 because of Pimple 3 and the fact that calling $app["swiftmailer.transport"] will freeze it so you cant change it. Row 22 and 24.

I dont have a fix for this yet. Writing here in hope that someone else does. :)

Edit: Found a solution. But I cant really 100% sure tell if its good enough. I think so ... But if anyone has anything else to say and finds this. Please let me know. :D

Change this line:
$app["swiftmailer.transport.original"] = $app["swiftmailer.transport"];

To:
$app["swiftmailer.transport.original"] = $app->raw("swiftmailer.transport");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment