2012-09-11 13 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)); 
    } 

あなたは$出力を削除する必要がある - > ...ライン(多分あなたは$送ら変数で有用な何かを行うことができます)。また、このコードは2種類のスプールを探しますが、あなたのスプールがこの種のものでない場合は、すべてのコードを必要としないかもしれません。

+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 –

関連する問題