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
+ 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 `_.
12
11
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
+ ------------
26
14
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 :
29
17
30
- .. configuration -block ::
18
+ .. code -block :: terminal
31
19
32
- .. code-block :: yaml
20
+ $ composer require mailer
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
+ If your application doesn't use Symfony Flex, follow the installation
23
+ instructions on `SwiftMailerBundle `_.
40
24
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 :
61
26
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
+ -------------
69
29
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:
73
34
74
- The following configuration attributes are available:
35
+ .. code-block :: bash
75
36
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
84
39
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
89
42
90
- .. caution ::
43
+ # use this to configure a traditional SMTP server
44
+ MAILER_URL=smtp://localhost:25? encryption=ssl& auth_mode=login& username=& password=
91
45
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.
95
48
96
49
Sending Emails
97
50
--------------
98
51
99
52
The Swift Mailer library works by creating, configuring and then sending
100
53
``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::
103
56
104
57
public function indexAction($name, \Swift_Mailer $mailer)
105
58
{
@@ -108,8 +61,8 @@ an email is pretty straightforward::
108
61
109
62
->setBody(
110
63
$this->renderView(
111
- // templates/Emails /registration.html.twig
112
- 'Emails /registration.html.twig',
64
+ // templates/emails /registration.html.twig
65
+ 'emails /registration.html.twig',
113
66
array('name' => $name)
114
67
),
115
68
'text/html'
@@ -118,7 +71,7 @@ an email is pretty straightforward::
118
71
* If you also want to include a plaintext version of the message
119
72
->addPart(
120
73
$this->renderView(
121
- 'Emails /registration.txt.twig',
74
+ 'emails /registration.txt.twig',
122
75
array('name' => $name)
123
76
),
124
77
'text/plain'
@@ -128,9 +81,6 @@ an email is pretty straightforward::
128
81
129
82
$mailer->send($message);
130
83
131
- // or, you can also fetch the mailer service this way
132
- // $this->get('mailer')->send($message);
133
-
134
84
return $this->render(...);
135
85
}
136
86
@@ -140,7 +90,7 @@ template might look something like this:
140
90
141
91
.. code-block :: html+jinja
142
92
143
- {# templates/Emails /registration.html.twig #}
93
+ {# templates/emails /registration.html.twig #}
144
94
<h3>You did it! You registered!</h3>
145
95
146
96
Hi {{ name }}! You're successfully registered.
@@ -154,19 +104,23 @@ template might look something like this:
154
104
<img src="{{ absolute_url(asset('images/logo.png')) }}">
155
105
156
106
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 .
159
109
160
110
Learn more
161
111
----------
162
112
163
113
.. toctree ::
164
114
:maxdepth: 1
165
- :glob:
166
115
167
- email/*
116
+ email/dev_environment
117
+ email/gmail
118
+ email/cloud
119
+ email/spool
120
+ email/testing
168
121
169
122
.. _`Swift Mailer` : http://swiftmailer.org/
123
+ .. _`SwiftMailerBundle` : https://github.com/symfony/swiftmailer-bundle
170
124
.. _`Creating Messages` : http://swiftmailer.org/docs/messages.html
171
125
.. _`Mandrill` : https://mandrill.com/
172
126
.. _`SendGrid` : https://sendgrid.com/
0 commit comments