-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Updated the main email article to Symfony 4/Flex #8652
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,102 +4,55 @@ | |
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. This bundle comes with the Symfony | ||
Standard Edition. | ||
Symfony provides a mailer feature based on the popular `Swift Mailer`_ library | ||
via the `SwiftMailerBundle`_. This mailer supports sending messages with your | ||
own mail servers as well as using popular email providers like `Mandrill`_, | ||
`SendGrid`_, and `Amazon SES`_. | ||
|
||
.. _swift-mailer-configuration: | ||
|
||
Configuration | ||
------------- | ||
|
||
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. | ||
Installation | ||
------------ | ||
|
||
In a standard Symfony installation, some ``swiftmailer`` configuration is | ||
already included: | ||
In applications using :doc:`Symfony Flex </setup/flex>`, execute this command to | ||
install and enable the mailer: | ||
|
||
.. configuration-block:: | ||
.. code-block:: terminal | ||
|
||
.. code-block:: yaml | ||
$ composer require mailer | ||
|
||
# app/config/config.yml | ||
swiftmailer: | ||
transport: '%mailer_transport%' | ||
host: '%mailer_host%' | ||
username: '%mailer_user%' | ||
password: '%mailer_password%' | ||
If your application doesn't use Symfony Flex, follow the installation | ||
instructions on `SwiftMailerBundle`_. | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd"> | ||
|
||
<swiftmailer:config | ||
transport="%mailer_transport%" | ||
host="%mailer_host%" | ||
username="%mailer_user%" | ||
password="%mailer_password%" | ||
/> | ||
</container> | ||
|
||
.. code-block:: php | ||
.. _swift-mailer-configuration: | ||
|
||
// app/config/config.php | ||
$container->loadFromExtension('swiftmailer', array( | ||
'transport' => "%mailer_transport%", | ||
'host' => "%mailer_host%", | ||
'username' => "%mailer_user%", | ||
'password' => "%mailer_password%", | ||
)); | ||
Configuration | ||
------------- | ||
|
||
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 ``config/packages/swiftmailer.yaml`` file that's created when installing the | ||
mailer provides all the initial config needed to send emails, except your mail | ||
server connection details. Those parameters are defined in the ``MAILER_URL`` | ||
environment variable in the ``.env`` file: | ||
|
||
The following configuration attributes are available: | ||
.. code-block:: bash | ||
|
||
* ``transport`` (``smtp``, ``mail``, ``sendmail``, or ``gmail``) | ||
* ``username`` | ||
* ``password`` | ||
* ``host`` | ||
* ``port`` | ||
* ``encryption`` (``tls``, or ``ssl``) | ||
* ``auth_mode`` (``plain``, ``login``, or ``cram-md5``) | ||
* ``spool`` | ||
# use this to disable email delivery | ||
MAILER_URL=null://localhost | ||
|
||
* ``type`` (how to queue the messages, ``file`` or ``memory`` is supported, see :doc:`/email/spool`) | ||
* ``path`` (where to store the messages) | ||
* ``delivery_addresses`` (an array of email addresses where to send ALL emails) | ||
* ``disable_delivery`` (set to true to disable delivery completely) | ||
# use this to send emails via Gmail (don't use this in production) | ||
MAILER_URL=gmail://username:password@localhost | ||
|
||
.. caution:: | ||
# use this to configure a traditional SMTP server | ||
MAILER_URL=smtp://localhost:25?encryption=ssl&auth_mode=login&username=&password= | ||
|
||
Starting from SwiftMailer 5.4.5, the ``mail`` transport is deprecated | ||
and will be removed in version 6. Consider using another transport like | ||
``smtp``, ``sendmail`` or ``gmail``. | ||
Refer to the :doc:`SwiftMailer configuration reference </reference/configuration/swiftmailer>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an easy example we could show for Mandrill, SendGrid or Amazon SES? I believe you basically use And if we show that here, could we delete There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, let's do that when we remove the other articles. |
||
for the detailed explanation of all the available config options. | ||
|
||
Sending Emails | ||
-------------- | ||
|
||
The Swift Mailer library works by creating, configuring and then sending | ||
``Swift_Message`` objects. The "mailer" is responsible for the actual delivery | ||
of the message and is accessible via the ``mailer`` service. Overall, sending | ||
an email is pretty straightforward:: | ||
of the message and is accessible via the ``Swift_Mailer`` service. Overall, | ||
sending an email is pretty straightforward:: | ||
|
||
public function indexAction($name, \Swift_Mailer $mailer) | ||
{ | ||
|
@@ -108,8 +61,8 @@ an email is pretty straightforward:: | |
->setTo('recipient@example.com') | ||
->setBody( | ||
$this->renderView( | ||
// templates/Emails/registration.html.twig | ||
'Emails/registration.html.twig', | ||
// templates/emails/registration.html.twig | ||
'emails/registration.html.twig', | ||
array('name' => $name) | ||
), | ||
'text/html' | ||
|
@@ -118,7 +71,7 @@ an email is pretty straightforward:: | |
* If you also want to include a plaintext version of the message | ||
->addPart( | ||
$this->renderView( | ||
'Emails/registration.txt.twig', | ||
'emails/registration.txt.twig', | ||
array('name' => $name) | ||
), | ||
'text/plain' | ||
|
@@ -128,9 +81,6 @@ an email is pretty straightforward:: | |
|
||
$mailer->send($message); | ||
|
||
// or, you can also fetch the mailer service this way | ||
// $this->get('mailer')->send($message); | ||
|
||
return $this->render(...); | ||
} | ||
|
||
|
@@ -140,7 +90,7 @@ template might look something like this: | |
|
||
.. code-block:: html+jinja | ||
|
||
{# templates/Emails/registration.html.twig #} | ||
{# templates/emails/registration.html.twig #} | ||
<h3>You did it! You registered!</h3> | ||
|
||
Hi {{ name }}! You're successfully registered. | ||
|
@@ -154,19 +104,23 @@ template might look something like this: | |
<img src="{{ absolute_url(asset('images/logo.png')) }}"> | ||
|
||
The ``$message`` object supports many more options, such as including attachments, | ||
adding HTML content, and much more. Fortunately, Swift Mailer covers the topic | ||
of `Creating Messages`_ in great detail in its documentation. | ||
adding HTML content, and much more. Refer to the `Creating Messages`_ section | ||
of the Swift Mailer documentation for more details. | ||
|
||
Learn more | ||
---------- | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
:glob: | ||
|
||
email/* | ||
email/dev_environment | ||
email/gmail | ||
email/cloud | ||
email/spool | ||
email/testing | ||
|
||
.. _`Swift Mailer`: http://swiftmailer.org/ | ||
.. _`SwiftMailerBundle`: https://github.com/symfony/swiftmailer-bundle | ||
.. _`Creating Messages`: http://swiftmailer.org/docs/messages.html | ||
.. _`Mandrill`: https://mandrill.com/ | ||
.. _`SendGrid`: https://sendgrid.com/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we / should we delete the
gmail.rst
article now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! I'm proposing to remove two email articles: #8668.