SendEmailCommand.php 2.6KB

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