Skip to content

Instantly share code, notes, and snippets.

@ricbra
Created January 9, 2015 21:32
Show Gist options
  • Save ricbra/7478a267296aeef45108 to your computer and use it in GitHub Desktop.
Save ricbra/7478a267296aeef45108 to your computer and use it in GitHub Desktop.
Example rabbitmq-cli-consumer command
namespace Wb\Bundle\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MailConsumerCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('wb:consumer:mail')
->addArgument('message', InputArgument::REQUIRED)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$message = $input->getArgument('message');
if (! $message = unserialize(base64_decode($message))) {
throw new \InvalidArgumentException('Invalid input received');
}
$container = $this->getContainer();
$mailer = $container->get('swiftmailer.mailer.mandrill');
$transport = $container->get('swiftmailer.mailer.mandrill.transport.real');
$logger = $container->get('rabbitmq_mail_logger');
$logger->info(sprintf(
'[%s] [%s] Received message from queue',
$message->getSubject(), implode(', ', array_keys($message->getTo()))
));
$spool = $mailer->getTransport()->getSpool();
$mailer->send($message);
$spool->flushQueue($transport);
$logger->info(sprintf(
'[%s] [%s] Mail send',
$message->getSubject(), implode(', ', array_keys($message->getTo()))
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment