@@ -7,43 +7,36 @@ How to Send an Email
7
7
Sending emails is a classic task for any web application and one that has
8
8
special complications and potential pitfalls. Instead of recreating the wheel,
9
9
one solution to send emails is to use the SwiftmailerBundle, which leverages
10
- the power of the `Swift Mailer `_ library.
11
-
12
- .. note ::
13
-
14
- Don't forget to enable the bundle in your kernel before using it::
15
-
16
- public function registerBundles()
17
- {
18
- $bundles = array(
19
- // ...
20
-
21
- new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
22
- );
23
-
24
- // ...
25
- }
10
+ the power of the `Swift Mailer `_ library. This bundle comes with the Symfony
11
+ Standard Edition.
26
12
27
13
.. _swift-mailer-configuration :
28
14
29
15
Configuration
30
16
-------------
31
17
32
- Before using Swift Mailer, be sure to include its configuration. The only
33
- mandatory configuration parameter is ``transport ``:
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.
26
+
27
+ In a standard Symfony installation, some ``swiftmailer `` configuration is
28
+ already included:
34
29
35
30
.. configuration-block ::
36
31
37
32
.. code-block :: yaml
38
33
39
34
# app/config/config.yml
40
35
swiftmailer :
41
- transport : smtp
42
- encryption : ssl
43
- auth_mode : login
44
- host : smtp.gmail.com
45
- username : your_username
46
- password : your_password
36
+ transport : " %mailer_transport%"
37
+ host : " %mailer_host%"
38
+ username : " %mailer_user%"
39
+ password : " %mailer_password%"
47
40
48
41
.. code-block :: xml
49
42
@@ -55,27 +48,24 @@ mandatory configuration parameter is ``transport``:
55
48
-->
56
49
57
50
<swiftmailer : config
58
- transport =" smtp"
59
- encryption =" ssl"
60
- auth-mode =" login"
61
- host =" smtp.gmail.com"
62
- username =" your_username"
63
- password =" your_password" />
51
+ transport =" %mailer_transport%"
52
+ host =" %mailer_host%"
53
+ username =" %mailer_user%"
54
+ password =" %mailer_password%" />
64
55
65
56
.. code-block :: php
66
57
67
58
// app/config/config.php
68
59
$container->loadFromExtension('swiftmailer', array(
69
- 'transport' => "smtp",
70
- 'encryption' => "ssl",
71
- 'auth_mode' => "login",
72
- 'host' => "smtp.gmail.com",
73
- 'username' => "your_username",
74
- 'password' => "your_password",
60
+ 'transport' => "%mailer_transport%",
61
+ 'host' => "%mailer_host%",
62
+ 'username' => "%mailer_user%",
63
+ 'password' => "%mailer_password%",
75
64
));
76
65
77
- The majority of the Swift Mailer configuration deals with how the messages
78
- themselves should be delivered.
66
+ These values (e.g. ``%mailer_transport% ``), are reading from the parameters
67
+ that are set in the :ref: `parameters.yml <config-parameters.yml >` file. You
68
+ can modify the values in that file, or set the values directly here.
79
69
80
70
The following configuration attributes are available:
81
71
@@ -105,15 +95,27 @@ an email is pretty straightforward::
105
95
{
106
96
$mailer = $this->get('mailer');
107
97
$message = $mailer->createMessage()
108
- ->setSubject('Hello Email ')
98
+ ->setSubject('You have Completed Registration! ')
109
99
110
100
111
101
->setBody(
112
102
$this->renderView(
113
- 'HelloBundle:Hello:email.txt.twig',
103
+ // app/Resources/views/Emails/registration.html.twig
104
+ 'Emails/registration.html.twig',
105
+ array('name' => $name)
106
+ ),
107
+ 'text/html'
108
+ )
109
+ /*
110
+ * If you also want to include a plaintext version of the message
111
+ ->addPart(
112
+ $this->renderView(
113
+ 'Emails/registration.txt.twig',
114
114
array('name' => $name)
115
- )
115
+ ),
116
+ 'text/plain'
116
117
)
118
+ */
117
119
;
118
120
$mailer->send($message);
119
121
@@ -138,3 +140,6 @@ of `Creating Messages`_ in great detail in its documentation.
138
140
139
141
.. _`Swift Mailer` : http://swiftmailer.org/
140
142
.. _`Creating Messages` : http://swiftmailer.org/docs/messages.html
143
+ .. _`Mandrill` : https://mandrill.com/
144
+ .. _`SendGrid` : https://sendgrid.com/
145
+ .. _`Amazon SES` : http://aws.amazon.com/ses/
0 commit comments