Skip to content

Commit 067da1e

Browse files
Adding working with email in development cookbook article
1 parent b2a2fe3 commit 067da1e

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

cookbook/email/dev_environment.rst

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
How to Work with Emails During Development
2+
==========================================
3+
4+
When you are creating an application which sends emails you will often
5+
not want to actually send the emails to the specified recipient during
6+
development. If you are using the ``SwiftmailerBundle`` with Symfony2 you
7+
can easily achieve this through configuration settings without having to
8+
make any changes to your application's code at all. There are two main
9+
choices to disable sending emails altogether and to send them all to a
10+
specified address.
11+
12+
Disabling Sending
13+
-----------------
14+
15+
You can disable sending emails by setting the ``disable_delivery`` option
16+
to ``true``. If you do this in the ``dev`` specific config then emails will
17+
not be sent when you are in the ``dev`` environment and will continue to be
18+
sent in the ``prod`` environment:
19+
20+
.. configuration-block::
21+
22+
.. code-block:: yaml
23+
24+
# app/config/config_dev.yml
25+
swiftmailer:
26+
disable_delivery: true
27+
28+
.. code-block:: xml
29+
30+
<!-- app/config/config_dev.xml -->
31+
32+
<!--
33+
xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
34+
http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd
35+
-->
36+
37+
<swiftmailer:config
38+
disable-delivery="true" />
39+
40+
.. code-block:: php
41+
42+
// app/config/config_dev.php
43+
$container->loadFromExtension('swiftmailer', array(
44+
'disable_delivery' => "true",
45+
));
46+
47+
48+
Sending to a Specifed Address
49+
-----------------------------
50+
51+
You can also choose to have all emails sent to an address set in the config,
52+
instead of the address they are set to be sent to, with the ``delivery_address``
53+
option:
54+
55+
.. configuration-block::
56+
57+
.. code-block:: yaml
58+
59+
# app/config/config_dev.yml
60+
swiftmailer:
61+
delivery_address: dev@example.com
62+
63+
.. code-block:: xml
64+
65+
<!-- app/config/config_dev.xml -->
66+
67+
<!--
68+
xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
69+
http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd
70+
-->
71+
72+
<swiftmailer:config
73+
delivery-address="dev@example.com" />
74+
75+
.. code-block:: php
76+
77+
// app/config/config_dev.php
78+
$container->loadFromExtension('swiftmailer', array(
79+
'delivery_address' => "dev@example.com",
80+
));
81+
82+
So if an email is sent from your app:
83+
84+
.. code-block:: php
85+
86+
public function indexAction($name)
87+
{
88+
$message = \Swift_Message::newInstance()
89+
->setSubject('Hello Email')
90+
->setFrom('send@example.com')
91+
->setTo('recipient@example.com')
92+
->setBody($this->renderView('HelloBundle:Hello:email.txt.twig', array('name' => $name)))
93+
;
94+
$this->get('mailer')->send($message);
95+
96+
return $this->render(...);
97+
}
98+
99+
Then instead of being sent to ``recipient@example.com`` it will be sent
100+
to ``dev@example.com``. Swiftmailer will add an extra header to the email,
101+
``X-Swift-To`` containing the replaced address, so you will still be able
102+
to see who it would have been sent to.
103+
104+
.. note::
105+
106+
As well as ``to`` addresses, this will also stop the email being sent
107+
to any ``CC`` and ``BCC`` addresses set for it. Swiftmailer will add
108+
additional headers to the email with the overridden addresses in them.
109+
These are ``X-Swift-Cc`` and ``X-Swift-Bcc`` for the ``CC`` and ``BCC``
110+
addresses respectively.
111+
112+
Viewing from the Web Debug Toolbar
113+
----------------------------------
114+
115+
You can view any emails sent by a page when you are in the ``dev`` environment
116+
using the Web Debug Toolbar. The email icon in the toolbar will show how
117+
many emails were sent. If you click it a report showing the details of the
118+
emails will open.

0 commit comments

Comments
 (0)