2012-09-11 31 views

回答

13

只要执行与命令相同的操作即可。从命令执行()函数:

$mailer  = $this->getContainer()->get('mailer'); 
    $transport = $mailer->getTransport(); 

    if ($transport instanceof \Swift_Transport_SpoolTransport) { 
     $spool = $transport->getSpool(); 
     if ($spool instanceof \Swift_ConfigurableSpool) { 
      $spool->setMessageLimit($input->getOption('message-limit')); 
      $spool->setTimeLimit($input->getOption('time-limit')); 
     } 
     if ($spool instanceof \Swift_FileSpool) { 
      if (null !== $input->getOption('recover-timeout')) { 
       $spool->recover($input->getOption('recover-timeout')); 
      } else { 
       $spool->recover(); 
      } 
     } 
     $sent = $spool->flushQueue($this->getContainer()->get('swiftmailer.transport.real')); 

     $output->writeln(sprintf('sent %s emails', $sent)); 
    } 

您需要删除$输出 - > ...线(也许你可以做一些与$送变电有用)。此外,这段代码寻找两种假脱机,也许你不需要所有的代码,如果你的假脱机不是这些类型之一。

+0

工作就像一个魅力:)谢谢卡洛斯 – Tom

22

这也可以通过How can I run symfony 2 run command from controller来实现,所以你不要复制代码。为我工作。

services.yml:

services: 
    swiftmailer.command.spool_send: 
     class: Symfony\Bundle\SwiftmailerBundle\Command\SendEmailCommand 
     calls: 
      - [ setContainer, ["@service_container"] ] 

控制器代码(简化):

$this->get('swiftmailer.command.spool_send')->run(new ArgvInput(array()), new ConsoleOutput()); 
+0

这绝对是更好的答案。 –

+0

是的,这是一个官方的方式来做到这一点 - http://symfony.com/doc/3.2/console/command_in_controller.html –

相关问题