|
| 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``. This is the default in the ``test`` environment in the Standard |
| 17 | +distribution. If you do this in the ``test`` specific config then emails |
| 18 | +will not be sent when you run tets and will continue to be sent in the |
| 19 | +``prod`` and ``dev`` environments: |
| 20 | + |
| 21 | +.. configuration-block:: |
| 22 | + |
| 23 | + .. code-block:: yaml |
| 24 | +
|
| 25 | + # app/config/config_test.yml |
| 26 | + swiftmailer: |
| 27 | + disable_delivery: true |
| 28 | +
|
| 29 | + .. code-block:: xml |
| 30 | +
|
| 31 | + <!-- app/config/config_test.xml --> |
| 32 | +
|
| 33 | + <!-- |
| 34 | + xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer" |
| 35 | + http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd |
| 36 | + --> |
| 37 | +
|
| 38 | + <swiftmailer:config |
| 39 | + disable-delivery="true" /> |
| 40 | +
|
| 41 | + .. code-block:: php |
| 42 | +
|
| 43 | + // app/config/config_test.php |
| 44 | + $container->loadFromExtension('swiftmailer', array( |
| 45 | + 'disable_delivery' => "true", |
| 46 | + )); |
| 47 | +
|
| 48 | +
|
| 49 | +Sending to a Specifed Address |
| 50 | +----------------------------- |
| 51 | + |
| 52 | +You can also choose to have all emails sent to an address set in the config, |
| 53 | +instead of the address they are set to be sent to, with the ``delivery_address`` |
| 54 | +option: |
| 55 | + |
| 56 | +.. configuration-block:: |
| 57 | + |
| 58 | + .. code-block:: yaml |
| 59 | +
|
| 60 | + # app/config/config_dev.yml |
| 61 | + swiftmailer: |
| 62 | + delivery_address: dev@example.com |
| 63 | +
|
| 64 | + .. code-block:: xml |
| 65 | +
|
| 66 | + <!-- app/config/config_dev.xml --> |
| 67 | +
|
| 68 | + <!-- |
| 69 | + xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer" |
| 70 | + http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd |
| 71 | + --> |
| 72 | +
|
| 73 | + <swiftmailer:config |
| 74 | + delivery-address="dev@example.com" /> |
| 75 | +
|
| 76 | + .. code-block:: php |
| 77 | +
|
| 78 | + // app/config/config_dev.php |
| 79 | + $container->loadFromExtension('swiftmailer', array( |
| 80 | + 'delivery_address' => "dev@example.com", |
| 81 | + )); |
| 82 | +
|
| 83 | +So if an email is sent from your app: |
| 84 | + |
| 85 | +.. code-block:: php |
| 86 | +
|
| 87 | + public function indexAction($name) |
| 88 | + { |
| 89 | + $message = \Swift_Message::newInstance() |
| 90 | + ->setSubject('Hello Email') |
| 91 | + ->setFrom('send@example.com') |
| 92 | + ->setTo('recipient@example.com') |
| 93 | + ->setBody($this->renderView('HelloBundle:Hello:email.txt.twig', array('name' => $name))) |
| 94 | + ; |
| 95 | + $this->get('mailer')->send($message); |
| 96 | +
|
| 97 | + return $this->render(...); |
| 98 | + } |
| 99 | +
|
| 100 | +Then instead of being sent to ``recipient@example.com`` it will be sent |
| 101 | +to ``dev@example.com``. Swiftmailer will add an extra header to the email, |
| 102 | +``X-Swift-To`` containing the replaced address, so you will still be able |
| 103 | +to see who it would have been sent to. |
| 104 | + |
| 105 | +.. note:: |
| 106 | + |
| 107 | + As well as ``to`` addresses, this will also stop the email being sent |
| 108 | + to any ``CC`` and ``BCC`` addresses set for it. Swiftmailer will add |
| 109 | + additional headers to the email with the overridden addresses in them. |
| 110 | + These are ``X-Swift-Cc`` and ``X-Swift-Bcc`` for the ``CC`` and ``BCC`` |
| 111 | + addresses respectively. |
| 112 | + |
| 113 | +Viewing from the Web Debug Toolbar |
| 114 | +---------------------------------- |
| 115 | + |
| 116 | +You can view any emails sent by a page when you are in the ``dev`` environment |
| 117 | +using the Web Debug Toolbar. The email icon in the toolbar will show how |
| 118 | +many emails were sent. If you click it a report showing the details of the |
| 119 | +emails will open. |
0 commit comments