vendor/symfony/swiftmailer-bundle/Command/SendEmailCommand.php line 97

Open in your IDE?
  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\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Style\SymfonyStyle;
  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.  * @author Toni Uebernickel <tuebernickel@gmail.com>
  21.  */
  22. class SendEmailCommand extends AbstractSwiftMailerCommand
  23. {
  24.     protected static $defaultName 'swiftmailer:spool:send';
  25.     /** @var SymfonyStyle */
  26.     private $io;
  27.     protected function configure()
  28.     {
  29.         $this
  30.             ->setName(static::$defaultName// BC with 2.7
  31.             ->setDescription('Sends emails from the spool')
  32.             ->addOption('message-limit'nullInputOption::VALUE_REQUIRED'The maximum number of messages to send.')
  33.             ->addOption('time-limit'nullInputOption::VALUE_REQUIRED'The time limit for sending messages (in seconds).')
  34.             ->addOption('recover-timeout'nullInputOption::VALUE_REQUIRED'The timeout for recovering messages that have taken too long to send (in seconds).')
  35.             ->addOption('mailer'nullInputOption::VALUE_REQUIRED'The mailer name.')
  36.             ->addOption('transport'nullInputOption::VALUE_REQUIRED'The service of the transport to use to send the messages.')
  37.             ->setHelp(
  38.                 <<<EOF
  39. The <info>%command.name%</info> command sends all emails from the spool.
  40. <info>php %command.full_name% --message-limit=10 --time-limit=10 --recover-timeout=900 --mailer=default</info>
  41. EOF
  42.             )
  43.         ;
  44.     }
  45.     /**
  46.      * @return int
  47.      */
  48.     protected function execute(InputInterface $inputOutputInterface $output)
  49.     {
  50.         $this->io = new SymfonyStyle($input$output);
  51.         $name $input->getOption('mailer');
  52.         if ($name) {
  53.             $this->processMailer($name$input$output);
  54.         } else {
  55.             $mailers array_keys($this->getContainer()->getParameter('swiftmailer.mailers'));
  56.             foreach ($mailers as $name) {
  57.                 $this->processMailer($name$input$output);
  58.             }
  59.         }
  60.         return 0;
  61.     }
  62.     private function processMailer($nameInputInterface $inputOutputInterface $output)
  63.     {
  64.         if (!$this->getContainer()->has(sprintf('swiftmailer.mailer.%s'$name))) {
  65.             throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.'$name));
  66.         }
  67.         $this->io->text(sprintf('<info>[%s]</info> Processing <info>%s</info> mailer spool... 'date('Y-m-d H:i:s'), $name));
  68.         if ($this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled'$name))) {
  69.             $mailer $this->getContainer()->get(sprintf('swiftmailer.mailer.%s'$name));
  70.             $transport $mailer->getTransport();
  71.             if ($transport instanceof \Swift_Transport_LoadBalancedTransport) {
  72.                 foreach ($transport->getTransports() as $eachTransport) {
  73.                     $this->recoverSpool($name$eachTransport$input$output);
  74.                 }
  75.             } else {
  76.                 $this->recoverSpool($name$transport$input$output);
  77.             }
  78.         } else {
  79.             $this->io->warning('There are no emails to send because the spool is disabled.');
  80.         }
  81.     }
  82.     private function recoverSpool($name, \Swift_Transport $transportInputInterface $input)
  83.     {
  84.         if ($transport instanceof \Swift_Transport_SpoolTransport) {
  85.             $spool $transport->getSpool();
  86.             if ($spool instanceof \Swift_ConfigurableSpool) {
  87.                 if (null !== $input->getOption('message-limit')) {
  88.                     $spool->setMessageLimit($input->getOption('message-limit'));
  89.                 }
  90.                 if (null !== $input->getOption('time-limit')) {
  91.                     $spool->setTimeLimit($input->getOption('time-limit'));
  92.                 }
  93.             }
  94.             if ($spool instanceof \Swift_FileSpool) {
  95.                 if (null !== $input->getOption('recover-timeout')) {
  96.                     $spool->recover($input->getOption('recover-timeout'));
  97.                 } else {
  98.                     $spool->recover();
  99.                 }
  100.             }
  101.             $transportService $input->getOption('transport') ?: sprintf('swiftmailer.mailer.%s.transport.real'$name);
  102.             $sent $spool->flushQueue($this->getContainer()->get($transportService));
  103.             $this->io->text(sprintf('<comment>%d</comment> emails sent'$sent));
  104.         }
  105.     }
  106. }