How to send spool from swiftmailer without using command

前端 未结 2 974
情话喂你
情话喂你 2021-02-15 14:55

How to send spool from swiftmailer without using command?

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

I need to put this somehow into php file

2条回答
  •  借酒劲吻你
    2021-02-15 15:39

    Just do the same that the command does. From the command Execute() function:

        $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));
        }
    

    You need to remove the $output->... line (maybe you can do something useful with the $sent variable). Also, this code looks for two kinds of spool, maybe you don´t need all the code if your spool is not one of these kinds.

提交回复
热议问题