Skip to content

Commit 98ea8a3

Browse files
committed
minor #8652 Updated the main email article to Symfony 4/Flex (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #8652). Discussion ---------- Updated the main email article to Symfony 4/Flex Commits ------- 9e10577 Changes after Ryan's review (thanks!) 1732ccc Fixed a RST reference 5708a75 Updated the main email article to Symfony 4/Flex
2 parents 1a3be43 + 9e10577 commit 98ea8a3

File tree

1 file changed

+42
-88
lines changed

1 file changed

+42
-88
lines changed

email.rst

Lines changed: 42 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,102 +4,55 @@
44
How 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+
Symfony provides a mailer feature based on the popular `Swift Mailer`_ library
8+
via the `SwiftMailerBundle`_. This mailer supports sending messages with your
9+
own mail servers as well as using popular email providers like `Mandrill`_,
10+
`SendGrid`_, and `Amazon SES`_.
1211

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.
12+
Installation
13+
------------
2614

27-
In a standard Symfony installation, some ``swiftmailer`` configuration is
28-
already included:
15+
In applications using :doc:`Symfony Flex </setup/flex>`, execute this command to
16+
install and enable the mailer:
2917

30-
.. configuration-block::
18+
.. code-block:: terminal
3119
32-
.. code-block:: yaml
20+
$ composer require mailer
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+
If your application doesn't use Symfony Flex, follow the installation
23+
instructions on `SwiftMailerBundle`_.
4024

41-
.. code-block:: xml
42-
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
25+
.. _swift-mailer-configuration:
6126

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-
));
27+
Configuration
28+
-------------
6929

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.
30+
The ``config/packages/swiftmailer.yaml`` file that's created when installing the
31+
mailer provides all the initial config needed to send emails, except your mail
32+
server connection details. Those parameters are defined in the ``MAILER_URL``
33+
environment variable in the ``.env`` file:
7334

74-
The following configuration attributes are available:
35+
.. code-block:: bash
7536
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``
37+
# use this to disable email delivery
38+
MAILER_URL=null://localhost
8439
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)
40+
# use this to send emails via Gmail (don't use this in production)
41+
MAILER_URL=gmail://username:password@localhost
8942
90-
.. caution::
43+
# use this to configure a traditional SMTP server
44+
MAILER_URL=smtp://localhost:25?encryption=ssl&auth_mode=login&username=&password=
9145
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``.
46+
Refer to the :doc:`SwiftMailer configuration reference </reference/configuration/swiftmailer>`
47+
for the detailed explanation of all the available config options.
9548

9649
Sending Emails
9750
--------------
9851

9952
The Swift Mailer library works by creating, configuring and then sending
10053
``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::
54+
of the message and is accessible via the ``Swift_Mailer`` service. Overall,
55+
sending an email is pretty straightforward::
10356

10457
public function indexAction($name, \Swift_Mailer $mailer)
10558
{
@@ -108,8 +61,8 @@ an email is pretty straightforward::
10861
->setTo('[email protected]')
10962
->setBody(
11063
$this->renderView(
111-
// templates/Emails/registration.html.twig
112-
'Emails/registration.html.twig',
64+
// templates/emails/registration.html.twig
65+
'emails/registration.html.twig',
11366
array('name' => $name)
11467
),
11568
'text/html'
@@ -118,7 +71,7 @@ an email is pretty straightforward::
11871
* If you also want to include a plaintext version of the message
11972
->addPart(
12073
$this->renderView(
121-
'Emails/registration.txt.twig',
74+
'emails/registration.txt.twig',
12275
array('name' => $name)
12376
),
12477
'text/plain'
@@ -128,9 +81,6 @@ an email is pretty straightforward::
12881

12982
$mailer->send($message);
13083

131-
// or, you can also fetch the mailer service this way
132-
// $this->get('mailer')->send($message);
133-
13484
return $this->render(...);
13585
}
13686

@@ -140,7 +90,7 @@ template might look something like this:
14090

14191
.. code-block:: html+jinja
14292

143-
{# templates/Emails/registration.html.twig #}
93+
{# templates/emails/registration.html.twig #}
14494
<h3>You did it! You registered!</h3>
14595

14696
Hi {{ name }}! You're successfully registered.
@@ -154,19 +104,23 @@ template might look something like this:
154104
<img src="{{ absolute_url(asset('images/logo.png')) }}">
155105

156106
The ``$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.
107+
adding HTML content, and much more. Refer to the `Creating Messages`_ section
108+
of the Swift Mailer documentation for more details.
159109

160110
Learn more
161111
----------
162112

163113
.. toctree::
164114
:maxdepth: 1
165-
:glob:
166115

167-
email/*
116+
email/dev_environment
117+
email/gmail
118+
email/cloud
119+
email/spool
120+
email/testing
168121

169122
.. _`Swift Mailer`: http://swiftmailer.org/
123+
.. _`SwiftMailerBundle`: https://github.com/symfony/swiftmailer-bundle
170124
.. _`Creating Messages`: http://swiftmailer.org/docs/messages.html
171125
.. _`Mandrill`: https://mandrill.com/
172126
.. _`SendGrid`: https://sendgrid.com/

0 commit comments

Comments
 (0)