44How to Send an Email
55====================
66
7- Sending emails is a classic task for any web application and one that has
8- special complications and potential pitfalls. Instead of recreating the wheel,
9- one solution to send emails is to use the SwiftmailerBundle, which leverages
10- the power of the `Swift Mailer `_ library. This bundle comes with the Symfony
11- Standard Edition.
7+ Sending emails is a common task for any web application and one that has
8+ special complications and potential pitfalls. Symfony provides a mailer feature
9+ based on the popular `Swift Mailer `_ library via the `SwiftMailerBundle `_.
1210
13- .. _swift-mailer-configuration :
14-
15- Configuration
16- -------------
17-
18- To use Swift Mailer, you'll need to configure it for your mail server.
19-
20- .. tip ::
21-
22- Instead of setting up/using your own mail server, you may want to use
23- a hosted mail provider such as `Mandrill `_, `SendGrid `_, `Amazon SES `_
24- or others. These give you an SMTP server, username and password (sometimes
25- called keys) that can be used with the Swift Mailer configuration.
11+ The Symfony mailer supports sending messages with your own mail servers as well
12+ as using popular email providers like `Mandrill `_, `SendGrid `_, and `Amazon SES `_.
2613
27- In a standard Symfony installation, some `` swiftmailer `` configuration is
28- already included:
14+ Installation
15+ ------------
2916
30- .. configuration-block ::
17+ In applications using :doc: `Symfony Flex </setup/flex >`, execute this command to
18+ install and enable the mailer:
3119
32- .. code-block :: yaml
20+ .. code-block :: terminal
3321
34- # app/config/config.yml
35- swiftmailer :
36- transport : ' %mailer_transport%'
37- host : ' %mailer_host%'
38- username : ' %mailer_user%'
39- password : ' %mailer_password%'
22+ $ composer require mailer
4023
41- .. code-block :: xml
24+ If your application doesn't use Symfony Flex, follow the installation
25+ instructions of the `SwiftMailerBundle `_.
4226
43- <!-- app/config/config.xml -->
44- <?xml version =" 1.0" encoding =" UTF-8" ?>
45- <container xmlns =" http://symfony.com/schema/dic/services"
46- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
47- xmlns : swiftmailer =" http://symfony.com/schema/dic/swiftmailer"
48- xsi : schemaLocation =" http://symfony.com/schema/dic/services
49- http://symfony.com/schema/dic/services/services-1.0.xsd
50- http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd" >
51-
52- <swiftmailer : config
53- transport =" %mailer_transport%"
54- host =" %mailer_host%"
55- username =" %mailer_user%"
56- password =" %mailer_password%"
57- />
58- </container >
59-
60- .. code-block :: php
27+ .. _swift-mailer-configuration :
6128
62- // app/config/config.php
63- $container->loadFromExtension('swiftmailer', array(
64- 'transport' => "%mailer_transport%",
65- 'host' => "%mailer_host%",
66- 'username' => "%mailer_user%",
67- 'password' => "%mailer_password%",
68- ));
29+ Configuration
30+ -------------
6931
70- These values (e.g. ``%mailer_transport% ``), are reading from the parameters
71- that are set in the :ref: `parameters.yml <config-parameters.yml >` file. You
72- can modify the values in that file, or set the values directly here.
32+ The ``config/packages/swiftmailer.yaml `` file created when installing the mailer
33+ provides all the initial config needed to make it work, except the parameters
34+ required to connect to the mail server. Those parameters are defined in the
35+ ``MAILER_URL `` environment variable in the ``.env `` file:
7336
74- The following configuration attributes are available:
37+ .. code-block :: bash
7538
76- * ``transport `` (``smtp ``, ``mail ``, ``sendmail ``, or ``gmail ``)
77- * ``username ``
78- * ``password ``
79- * ``host ``
80- * ``port ``
81- * ``encryption `` (``tls ``, or ``ssl ``)
82- * ``auth_mode `` (``plain ``, ``login ``, or ``cram-md5 ``)
83- * ``spool ``
39+ # use this to disable the email delivery
40+ MAILER_URL=null://localhost
8441
85- * ``type `` (how to queue the messages, ``file `` or ``memory `` is supported, see :doc: `/email/spool `)
86- * ``path `` (where to store the messages)
87- * ``delivery_addresses `` (an array of email addresses where to send ALL emails)
88- * ``disable_delivery `` (set to true to disable delivery completely)
42+ # use this to send emails via Gmail (don't use this in production)
43+ MAILER_URL=gmail://username:password@localhost
8944
90- .. caution ::
45+ # use this to configure a traditional SMTP server
46+ MAILER_URL=smtp://localhost:25? encryption=ssl& auth_mode=login& username=& password=
9147
92- Starting from SwiftMailer 5.4.5, the ``mail `` transport is deprecated
93- and will be removed in version 6. Consider using another transport like
94- ``smtp ``, ``sendmail `` or ``gmail ``.
48+ Refer to the :doc: `SwiftMailer configuration reference </reference/configuration/swiftmailer.yml >`
49+ for the detailed explanation of all the available config options.
9550
9651Sending Emails
9752--------------
9853
9954The Swift Mailer library works by creating, configuring and then sending
10055``Swift_Message `` objects. The "mailer" is responsible for the actual delivery
101- of the message and is accessible via the ``mailer `` service. Overall, sending
102- an email is pretty straightforward::
56+ of the message and is accessible via the ``Swift_Mailer `` service. Overall,
57+ sending an email is pretty straightforward::
10358
10459 public function indexAction($name, \Swift_Mailer $mailer)
10560 {
@@ -108,8 +63,8 @@ an email is pretty straightforward::
10863 ->setTo('recipient@example.com')
10964 ->setBody(
11065 $this->renderView(
111- // templates/Emails /registration.html.twig
112- 'Emails /registration.html.twig',
66+ // templates/emails /registration.html.twig
67+ 'emails /registration.html.twig',
11368 array('name' => $name)
11469 ),
11570 'text/html'
@@ -118,7 +73,7 @@ an email is pretty straightforward::
11873 * If you also want to include a plaintext version of the message
11974 ->addPart(
12075 $this->renderView(
121- 'Emails /registration.txt.twig',
76+ 'emails /registration.txt.twig',
12277 array('name' => $name)
12378 ),
12479 'text/plain'
@@ -128,9 +83,6 @@ an email is pretty straightforward::
12883
12984 $mailer->send($message);
13085
131- // or, you can also fetch the mailer service this way
132- // $this->get('mailer')->send($message);
133-
13486 return $this->render(...);
13587 }
13688
@@ -140,7 +92,7 @@ template might look something like this:
14092
14193.. code-block :: html+jinja
14294
143- {# templates/Emails /registration.html.twig #}
95+ {# templates/emails /registration.html.twig #}
14496 <h3>You did it! You registered!</h3>
14597
14698 Hi {{ name }}! You're successfully registered.
@@ -154,19 +106,23 @@ template might look something like this:
154106 <img src="{{ absolute_url(asset('images/logo.png')) }}">
155107
156108The ``$message `` object supports many more options, such as including attachments,
157- adding HTML content, and much more. Fortunately, Swift Mailer covers the topic
158- of ` Creating Messages `_ in great detail in its documentation .
109+ adding HTML content, and much more. Refer to the ` Creating Messages `_ section
110+ of the Swift Mailer documentation for more details .
159111
160112Learn more
161113----------
162114
163115.. toctree ::
164116 :maxdepth: 1
165- :glob:
166117
167- email/*
118+ email/dev_environment
119+ email/gmail
120+ email/cloud
121+ email/spool
122+ email/testing
168123
169124.. _`Swift Mailer` : http://swiftmailer.org/
125+ .. _`SwiftMailerBundle` : https://github.com/symfony/swiftmailer-bundle
170126.. _`Creating Messages` : http://swiftmailer.org/docs/messages.html
171127.. _`Mandrill` : https://mandrill.com/
172128.. _`SendGrid` : https://sendgrid.com/
0 commit comments