Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. How to Send an Email
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Configuration
  • Sending Emails
  • Learn more

How to Send an Email

Edit this page

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

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

How to Send an Email

Sending emails is a classic task for any web application and one that has special complications and potential pitfalls. Instead of recreating the wheel, one solution to send emails is to use the SwiftmailerBundle, which leverages the power of the Swift Mailer library. This bundle comes with the Symfony Standard Edition.

Configuration

To use Swift Mailer, you'll need to configure it for your mail server.

Tip

Instead of setting up/using your own mail server, you may want to use a hosted mail provider such as Mandrill, SendGrid, Amazon SES or others. These give you an SMTP server, username and password (sometimes called keys) that can be used with the Swift Mailer configuration.

In a standard Symfony installation, some swiftmailer configuration is already included:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
# app/config/config.yml
swiftmailer:
    transport: '%mailer_transport%'
    host:      '%mailer_host%'
    username:  '%mailer_user%'
    password:  '%mailer_password%'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd">

    <swiftmailer:config
        transport="%mailer_transport%"
        host="%mailer_host%"
        username="%mailer_user%"
        password="%mailer_password%"
    />
</container>
1
2
3
4
5
6
7
// app/config/config.php
$container->loadFromExtension('swiftmailer', array(
    'transport'  => "%mailer_transport%",
    'host'       => "%mailer_host%",
    'username'   => "%mailer_user%",
    'password'   => "%mailer_password%",
));

These values (e.g. %mailer_transport%), are reading from the parameters that are set in the parameters.yml file. You can modify the values in that file, or set the values directly here.

The following configuration attributes are available:

  • transport (smtp, mail, sendmail, or gmail)
  • username
  • password
  • host
  • port
  • encryption (tls, or ssl)
  • auth_mode (plain, login, or cram-md5)
  • spool

    • type (how to queue the messages, file or memory is supported, see How to Spool Emails)
    • path (where to store the messages)
  • delivery_addresses (an array of email addresses where to send ALL emails)
  • disable_delivery (set to true to disable delivery completely)

Caution

Starting from SwiftMailer 5.4.5, the mail transport is deprecated and will be removed in version 6. Consider using another transport like smtp, sendmail or gmail.

Sending Emails

The Swift Mailer library works by creating, configuring and then sending Swift_Message objects. The "mailer" is responsible for the actual delivery of the message and is accessible via the mailer service. Overall, sending an email is pretty straightforward:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public function indexAction($name)
{
    $message = (new \Swift_Message('Hello Email'))
        ->setFrom('send@example.com')
        ->setTo('recipient@example.com')
        ->setBody(
            $this->renderView(
                // app/Resources/views/Emails/registration.html.twig
                'Emails/registration.html.twig',
                array('name' => $name)
            ),
            'text/html'
        )
        /*
         * If you also want to include a plaintext version of the message
        ->addPart(
            $this->renderView(
                'Emails/registration.txt.twig',
                array('name' => $name)
            ),
            'text/plain'
        )
        */
    ;

    $this->get('mailer')->send($message);

    return $this->render(...);
}

To keep things decoupled, the email body has been stored in a template and rendered with the renderView() method. The registration.html.twig template might look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
{# app/Resources/views/Emails/registration.html.twig #}
<h3>You did it! You registered!</h3>

Hi {{ name }}! You're successfully registered.

{# example, assuming you have a route named "login" #}
To login, go to: <a href="{{ url('login') }}">...</a>.

Thanks!

{# Makes an absolute URL to the /images/logo.png file #}
<img src="{{ absolute_url(asset('images/logo.png')) }}">

2.7

The absolute_url() function was introduced in Symfony 2.7. Prior to 2.7, the asset() function has an argument to enable returning an absolute URL.

The $message object supports many more options, such as including attachments, adding HTML content, and much more. Fortunately, Swift Mailer covers the topic of Creating Messages in great detail in its documentation.

Learn more

  • How to Use the Cloud to Send Emails
  • How to Work with Emails during Development
  • How to Use Gmail to Send Emails
  • How to Spool Emails
  • How to Test that an Email is Sent in a Functional Test
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    The life jacket for your team and your project

    The life jacket for your team and your project

    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Symfony footer

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

    Avatar of Sergii Dolgushev, a Symfony contributor

    Thanks Sergii Dolgushev (@serhey) for being a Symfony contributor

    3 commits • 17 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