2012-09-11 16 views
8

कमांड का उपयोग किये बिना स्विफ्टमेलर से स्पूल कैसे भेजें, बिना आदेश के स्विफ्टमेलर से स्पूल कैसे भेजें?कमांड

php app/console swiftmailer:spool:send --env=prod

मैं इस किसी भी तरह php फ़ाइल में डाल करने के लिए इतना है कि सर्वर व्यवस्थापक इस अनुसूची में जोड़ सकते हैं की जरूरत है।

उत्तर

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 –