How to send spool from swiftmailer without using command
Asked Answered
J

2

8

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 so that Server admin can add this to Schedule.

Jenellejenesia answered 11/9, 2012 at 12:17 Comment(0)
B
14

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.

Barbed answered 11/9, 2012 at 12:29 Comment(1)
Worked like a charm :) Thank you CarlosJenellejenesia
R
22

This can also be achieved by How can I run symfony 2 run command from controller , so you don't duplicate code. Worked for me.

services.yml:

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

Controller code (simplified):

$this->get('swiftmailer.command.spool_send')->run(new ArgvInput(array()), new ConsoleOutput());
Rapture answered 5/1, 2013 at 14:8 Comment(2)
This is definitely the better answer.Dragging
Yes, and this is an official way to do this - symfony.com/doc/3.2/console/command_in_controller.htmlDynamism
B
14

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.

Barbed answered 11/9, 2012 at 12:29 Comment(1)
Worked like a charm :) Thank you CarlosJenellejenesia

© 2022 - 2024 — McMap. All rights reserved.