Skip to content

Commit 3cbbd66

Browse files
committed
Merge remote branch 'dator/send_email_command'
* dator/send_email_command: Fix description Add an example using the options in the help message Fix CS [SwiftMailer] Add the SendEmail Command (for the spool)
2 parents f6e624b + e8d39d6 commit 3cbbd66

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\SwiftmailerBundle\Command;
13+
14+
use Symfony\Bundle\FrameworkBundle\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Input\InputOption;
18+
19+
/**
20+
* Send Emails from the spool.
21+
*
22+
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
23+
* @author Clément JOBEILI <clement.jobeili@gmail.com>
24+
*/
25+
class SendEmailCommand extends Command
26+
{
27+
/**
28+
* @see Command
29+
*/
30+
protected function configure()
31+
{
32+
$this
33+
->setName('swiftmailer:spool:send')
34+
->setDescription('Send emails from the spool')
35+
->addOption('message-limit', 0, InputOption::VALUE_OPTIONAL, 'The maximum number of messages to send.')
36+
->addOption('time-limit', 0, InputOption::VALUE_OPTIONAL, 'The time limit for sending messages (in seconds).')
37+
->setHelp(<<<EOF
38+
The <info>swiftmailer:spool:send</info> command send all emails from the spool.
39+
40+
<info>./app/console swiftmailer:spool:send --message-limit=10 --time-limit=10</info>
41+
42+
EOF
43+
)
44+
;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
protected function execute(InputInterface $input, OutputInterface $output)
51+
{
52+
$mailer = $this->container->get('mailer');
53+
$transport = $mailer->getTransport();
54+
55+
if ($transport instanceof \Swift_Transport_SpoolTransport) {
56+
$spool = $transport->getSpool();
57+
$spool->setMessageLimit($input->getOption('message-limit'));
58+
$spool->setTimeLimit($input->getOption('time-limit'));
59+
$sent = $spool->flushQueue($this->container->get('swiftmailer.transport.real'));
60+
61+
$output->writeln(sprintf('sent %s emails', $sent));
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)