Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Cookbook
  4. Email
  5. How to Spool Emails
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Spool Using Memory
  • Spool Using a File

How to Spool Emails

Edit this page

Warning: You are browsing the documentation for Symfony 2.5, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

How to Spool Emails

When you are using the SwiftmailerBundle to send an email from a Symfony application, it will default to sending the email immediately. You may, however, want to avoid the performance hit of the communication between Swift Mailer and the email transport, which could cause the user to wait for the next page to load while the email is sending. This can be avoided by choosing to "spool" the emails instead of sending them directly. This means that Swift Mailer does not attempt to send the email but instead saves the message to somewhere such as a file. Another process can then read from the spool and take care of sending the emails in the spool. Currently only spooling to file or memory is supported by Swift Mailer.

Spool Using Memory

When you use spooling to store the emails to memory, they will get sent right before the kernel terminates. This means the email only gets sent if the whole request got executed without any unhandled Exception or any errors. To configure swiftmailer with the memory option, use the following configuration:

  • YAML
  • XML
  • PHP
1
2
3
4
# app/config/config.yml
swiftmailer:
    # ...
    spool: { type: memory }
1
2
3
4
5
6
7
8
9
10
11
<!-- app/config/config.xml -->

<!--
    xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
    http://symfony.com/schema/dic/swiftmailer
    http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd
-->

<swiftmailer:config>
     <swiftmailer:spool type="memory" />
</swiftmailer:config>
1
2
3
4
5
// app/config/config.php
$container->loadFromExtension('swiftmailer', array(
     // ...
    'spool' => array('type' => 'memory')
));

Spool Using a File

In order to use the spool with a file, use the following configuration:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
# app/config/config.yml
swiftmailer:
    # ...
    spool:
        type: file
        path: /path/to/spool
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- app/config/config.xml -->

<!--
    xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
    http://symfony.com/schema/dic/swiftmailer
    http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd
-->

<swiftmailer:config>
     <swiftmailer:spool
         type="file"
         path="/path/to/spool" />
</swiftmailer:config>
1
2
3
4
5
6
7
8
9
// app/config/config.php
$container->loadFromExtension('swiftmailer', array(
     // ...

    'spool' => array(
        'type' => 'file',
        'path' => '/path/to/spool',
    ),
));

Tip

If you want to store the spool somewhere with your project directory, remember that you can use the %kernel.root_dir% parameter to reference the project's root:

1
path: "%kernel.root_dir%/spool"

Now, when your app sends an email, it will not actually be sent but instead added to the spool. Sending the messages from the spool is done separately. There is a console command to send the messages in the spool:

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

It has an option to limit the number of messages to be sent:

1
$ php app/console swiftmailer:spool:send --message-limit=10 --env=prod

You can also set the time limit in seconds:

1
$ php app/console swiftmailer:spool:send --time-limit=10 --env=prod

Of course you will not want to run this manually in reality. Instead, the console command should be triggered by a cron job or scheduled task and run at a regular interval.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Take the exam at home

Take the exam at home

Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Nicolas Séverin, a Symfony contributor

Thanks Nicolas Séverin for being a Symfony contributor

1 commit • 5 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia