Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick proofread of the email cookbook #4918

Merged
merged 3 commits into from
Jan 30, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions best_practices/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and security credentials) and different environments (development, production).
That's why Symfony recommends that you split the application configuration into
three parts.

.. _config-parameters.yml:

Infrastructure-Related Configuration
------------------------------------

Expand Down
87 changes: 46 additions & 41 deletions cookbook/email/email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,36 @@ How to Send an Email
Sending emails is a classic task for any web application and one that has
special complications and potential pitfalls. Instead of recreating the wheel,
one solution to send emails is to use the SwiftmailerBundle, which leverages
the power of the `Swift Mailer`_ library.

.. note::

Don't forget to enable the bundle in your kernel before using it::

public function registerBundles()
{
$bundles = array(
// ...

new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
);

// ...
}
the power of the `Swift Mailer`_ library. This bundles comes with the Symfony
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bundle

Standard Edition.

.. _swift-mailer-configuration:

Configuration
-------------

Before using Swift Mailer, be sure to include its configuration. The only
mandatory configuration parameter is ``transport``:
To use Swift Mailer, you'll need to configure it for your mail server.

.. tip::

Instead of setting up/using your own mail server, you may want to use
a hosted mail provider such as `Mandrill`_, `SendGrid`_, `Amazon SES`_
or others. These give you an SMTP server, username and password (sometimes
called keys) that can be used with the Swift Mailer configuration.

In a standard Symfony installation, some ``swiftmailer`` configuration is
already included:

.. configuration-block::

.. code-block:: yaml

# app/config/config.yml
swiftmailer:
transport: smtp
encryption: ssl
auth_mode: login
host: smtp.gmail.com
username: your_username
password: your_password
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"

.. code-block:: xml

Expand All @@ -55,27 +48,24 @@ mandatory configuration parameter is ``transport``:
-->

<swiftmailer:config
transport="smtp"
encryption="ssl"
auth-mode="login"
host="smtp.gmail.com"
username="your_username"
password="your_password" />
transport="%mailer_transport%"
host="%mailer_host%"
username="%mailer_user%"
password="%mailer_password%" />

.. code-block:: php

// app/config/config.php
$container->loadFromExtension('swiftmailer', array(
'transport' => "smtp",
'encryption' => "ssl",
'auth_mode' => "login",
'host' => "smtp.gmail.com",
'username' => "your_username",
'password' => "your_password",
'transport' => "%mailer_transport%",
'host' => "%mailer_host%",
'username' => "%mailer_user%",
'password' => "%mailer_password%",
));

The majority of the Swift Mailer configuration deals with how the messages
themselves should be delivered.
These values (e.g. ``%mailer_transport%``), are reading from the parameters
that are set in the :ref:`parameters.yml <config-parameters.yml>` file. You
can modify the values in that file, or set the values directly here.

The following configuration attributes are available:

Expand Down Expand Up @@ -105,15 +95,27 @@ an email is pretty straightforward::
{
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject('Hello Email')
->setSubject('You have Completed Registration!')
->setFrom('[email protected]')
->setTo('[email protected]')
->setBody(
$this->renderView(
'HelloBundle:Hello:email.txt.twig',
// app/Resources/views/Emails/registration.html.twig
'Emails/registration.html.twig',
array('name' => $name)
),
'text/html'
)
/*
* If you also want to include a plaintext version of the message
->addPart(
$this->renderView(
'Emails/registration.txt.twig',
array('name' => $name)
)
),
'text/plain'
)
*/
;
$mailer->send($message);

Expand All @@ -138,3 +140,6 @@ of `Creating Messages`_ in great detail in its documentation.

.. _`Swift Mailer`: http://swiftmailer.org/
.. _`Creating Messages`: http://swiftmailer.org/docs/messages.html
.. _`Mandrill`: https://mandrill.com/
.. _`SendGrid`: https://sendgrid.com/
.. _`Amazon SES`: http://aws.amazon.com/ses/