Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

WARNING: You are browsing the documentation for Symfony 2.0 which is not maintained anymore. Consider upgrading your projects to Symfony 5.1.

How to generate URLs and send Emails from the Console

2.0 version

How to generate URLs and send Emails from the Console

Unfortunately, the command line context does not know about your VirtualHost or domain name. This means that if if you generate absolute URLs within a Console Command you’ll probably end up with something like http://localhost/foo/bar which is not very useful.

To fix this, you need to configure the “request context”, which is a fancy way of saying that you need to configure your environment so that it knows what URL it should use when generating URLs.

There are two ways of configuring the request context: at the application level (only available in Symfony 2.1+) and per Command.

Configuring the Request Context per Command

To change it only in one command you can simply fetch the Request Context service and override its settings:

// src/Acme/DemoBundle/Command/DemoCommand.php

// ...
class DemoCommand extends ContainerAwareCommand
    protected function execute(InputInterface $input, OutputInterface $output)
        $context = $this->getContainer()->get('router')->getContext();

        // ... your code here

Using Memory Spooling

Sending emails in a console command works the same way as described in the How to send an Email cookbook except if memory spooling is used.

When using memory spooling (see the How to Spool Emails cookbook for more information), you must be aware that because of how symfony handles console commands, emails are not sent automatically. You must take care of flushing the queue yourself. Use the following code to send emails inside your console command:

$container = $this->getContainer();
$mailer = $container->get('mailer');
$spool = $mailer->getTransport()->getSpool();
$transport = $container->get('swiftmailer.transport.real');


Another option is to create an environment which is only used by console commands and uses a different spooling method.


Taking care of the spooling is only needed when memory spooling is used. If you are using file spooling (or no spooling at all), there is no need to flush the queue manually within the command.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.