-
-
Save wgetus/35bedb0b4663c9f4e5215bed081cf88d to your computer and use it in GitHub Desktop.
Sending emails in Silex via Swiftmailer spool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//...your silex bootstrap. | |
//Configure Swiftmail to use SMTP. | |
$app->register(new SwiftmailerExtension(), array( | |
'swiftmailer.options' => array( | |
'host' => 'smtp.gmail.com', | |
'port' => 465, | |
'username' => '[email protected]', | |
'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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment