4
4
How to Send an Email
5
5
====================
6
6
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 `_.
12
10
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 `_.
26
13
27
- In a standard Symfony installation, some `` swiftmailer `` configuration is
28
- already included:
14
+ Installation
15
+ ------------
29
16
30
- .. configuration-block ::
17
+ In applications using :doc: `Symfony Flex </setup/flex >`, execute this command to
18
+ install and enable the mailer:
31
19
32
- .. code-block :: yaml
20
+ .. code-block :: terminal
33
21
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
40
23
41
- .. code-block :: xml
24
+ If your application doesn't use Symfony Flex, follow the installation
25
+ instructions of the `SwiftMailerBundle `_.
42
26
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 :
61
28
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
+ -------------
69
31
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:
73
36
74
- The following configuration attributes are available:
37
+ .. code-block :: bash
75
38
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
84
41
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
89
44
90
- .. caution ::
45
+ # use this to configure a traditional SMTP server
46
+ MAILER_URL=smtp://localhost:25? encryption=ssl& auth_mode=login& username=& password=
91
47
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.
95
50
96
51
Sending Emails
97
52
--------------
98
53
99
54
The Swift Mailer library works by creating, configuring and then sending
100
55
``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::
103
58
104
59
public function indexAction($name, \Swift_Mailer $mailer)
105
60
{
@@ -108,8 +63,8 @@ an email is pretty straightforward::
108
63
->setTo('recipient@example.com')
109
64
->setBody(
110
65
$this->renderView(
111
- // templates/Emails /registration.html.twig
112
- 'Emails /registration.html.twig',
66
+ // templates/emails /registration.html.twig
67
+ 'emails /registration.html.twig',
113
68
array('name' => $name)
114
69
),
115
70
'text/html'
@@ -118,7 +73,7 @@ an email is pretty straightforward::
118
73
* If you also want to include a plaintext version of the message
119
74
->addPart(
120
75
$this->renderView(
121
- 'Emails /registration.txt.twig',
76
+ 'emails /registration.txt.twig',
122
77
array('name' => $name)
123
78
),
124
79
'text/plain'
@@ -128,9 +83,6 @@ an email is pretty straightforward::
128
83
129
84
$mailer->send($message);
130
85
131
- // or, you can also fetch the mailer service this way
132
- // $this->get('mailer')->send($message);
133
-
134
86
return $this->render(...);
135
87
}
136
88
@@ -140,7 +92,7 @@ template might look something like this:
140
92
141
93
.. code-block :: html+jinja
142
94
143
- {# templates/Emails /registration.html.twig #}
95
+ {# templates/emails /registration.html.twig #}
144
96
<h3>You did it! You registered!</h3>
145
97
146
98
Hi {{ name }}! You're successfully registered.
@@ -154,19 +106,23 @@ template might look something like this:
154
106
<img src="{{ absolute_url(asset('images/logo.png')) }}">
155
107
156
108
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 .
109
+ adding HTML content, and much more. Refer to the ` Creating Messages `_ section
110
+ of the Swift Mailer documentation for more details .
159
111
160
112
Learn more
161
113
----------
162
114
163
115
.. toctree ::
164
116
:maxdepth: 1
165
- :glob:
166
117
167
- email/*
118
+ email/dev_environment
119
+ email/gmail
120
+ email/cloud
121
+ email/spool
122
+ email/testing
168
123
169
124
.. _`Swift Mailer` : http://swiftmailer.org/
125
+ .. _`SwiftMailerBundle` : https://github.com/symfony/swiftmailer-bundle
170
126
.. _`Creating Messages` : http://swiftmailer.org/docs/messages.html
171
127
.. _`Mandrill` : https://mandrill.com/
172
128
.. _`SendGrid` : https://sendgrid.com/
0 commit comments