-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Initial mailer component docs #11626
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e2c89df
Initial mailer component docs
harikt bb4c064
Incorporate changes requested by @weaverryan
harikt 6e59aa6
Code level changes, as suggested by @wouterj
harikt 83477ab
update with changes and same format as currently merged mailer
harikt 9f81783
Update components/mailer.rst
harikt 1b2b4d6
Update components/mailer.rst as per suggestion.
harikt 8a680bd
Add learn more
harikt f17952c
Remove extra line
harikt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,140 @@ Installation | |
|
||
.. include:: /components/require_autoload.rst.inc | ||
|
||
|
||
Introduction | ||
------------ | ||
|
||
Symfony mailer is an experimental component introduced in 4.3 which | ||
will eventually replace swiftmailer. | ||
|
||
|
||
Usage | ||
----- | ||
|
||
We're currently working on the documentation of this component that was just | ||
added to Symfony. We'll publish it in a few days. | ||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The Mailer component has two main classes: a ``Transport`` and the ``Mailer`` itself:: | ||
|
||
use Symfony\Component\Mailer\Mailer; | ||
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport; | ||
|
||
$transport = new SmtpTransport('localhost'); | ||
$mailer = new Mailer($transport); | ||
$mailer->send($email); | ||
|
||
The `$email` object is created via the :doc:`Mime component </components/mime>`. | ||
|
||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Transport | ||
--------- | ||
|
||
The only transport that comes pre-installed with mailer is Smtp. | ||
|
||
Below is the list of other popular providers with built in support. | ||
|
||
================== ============================================= | ||
Service Install with | ||
================== ============================================= | ||
Amazon SES ``composer require symfony/amazon-mailer`` | ||
Gmail ``composer require symfony/google-mailer`` | ||
MailChimp ``composer require symfony/mailchimp-mailer`` | ||
Mailgun ``composer require symfony/mailgun-mailer`` | ||
Postmark ``composer require symfony/postmark-mailer`` | ||
SendGrid ``composer require symfony/sendgrid-mailer`` | ||
================== ============================================= | ||
|
||
For example, suppose you want to use Google's Gmail. First, install it: | ||
|
||
.. code-block:: terminal | ||
|
||
$ composer require symfony/google-mailer | ||
|
||
.. code-block:: php | ||
|
||
use Symfony\Component\Mailer\Bridge\Google\Smtp\GmailTransport; | ||
wouterj marked this conversation as resolved.
Show resolved
Hide resolved
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$transport = new GmailTransport('user', 'pass'); | ||
$mailer = new Mailer($transport); | ||
$mailer->send($email); | ||
|
||
Use a DSN | ||
--------- | ||
|
||
The mailer component provides a convenient way to create transport object from DSN string:: | ||
|
||
use Symfony\Component\Mailer\Transport; | ||
|
||
$transport = Transport::fromDsn($dsn); | ||
|
||
Where ``$dsn`` as one of the form below. | ||
|
||
- ``smtp://user:pass@gmail`` | ||
- ``smtp://key@sendgrid`` | ||
- ``smtp://null`` | ||
- ``smtp://user:pass@mailgun`` | ||
- ``http://key:domain@mailgun`` | ||
- ``api://id@postmark`` | ||
|
||
This provides a unified behaviour across all providers. | ||
Easily switch from SMTP in development to a "real" provider in production with same API. | ||
|
||
Failover transport | ||
------------------ | ||
|
||
You can create failover transport with the help of `||` operator:: | ||
|
||
$dsn = 'api://id@postmark || smtp://key@sendgrid'; | ||
|
||
So if the first transport fails, the mailer will attempt to send through the second transport. | ||
|
||
Round Robin | ||
----------- | ||
|
||
If you want to send emails by using multiple transports in a round-robin fashion, you can use the | ||
``&&`` operator between the transports:: | ||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$dsn = 'api://id@postmark && smtp://key@sendgrid' | ||
|
||
Async | ||
----- | ||
|
||
If you want to use the async functionality you need to install the :doc:`Messenger component </components/messenger>`. | ||
|
||
.. code-block:: terminal | ||
|
||
$ composer require symfony/messenger | ||
wouterj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Then, instantiate and pass a ``MessageBus`` as a second argument to ``Mailer``:: | ||
|
||
use Symfony\Component\Mailer\Mailer; | ||
use Symfony\Component\Mailer\Messenger\MessageHandler; | ||
use Symfony\Component\Mailer\Messenger\SendEmailMessage; | ||
use Symfony\Component\Mailer\SmtpEnvelope; | ||
use Symfony\Component\Mailer\Transport; | ||
use Symfony\Component\Messenger\Handler\HandlersLocator; | ||
use Symfony\Component\Messenger\MessageBus; | ||
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware; | ||
use Symfony\Component\Mime\Address; | ||
|
||
$dsn = 'change-dsn-accordingly'; | ||
|
||
$transport = Transport::fromDsn($dsn); | ||
$handler = new MessageHandler($transport); | ||
|
||
$bus = new MessageBus([ | ||
new HandleMessageMiddleware(new HandlersLocator([ | ||
SendEmailMessage::class => [$handler], | ||
])), | ||
]); | ||
|
||
$mailer = new Mailer($transport, $bus); | ||
|
||
$mailer->send($email, new SmtpEnvelope( | ||
new Address('[email protected]'), | ||
[ | ||
new Address('[email protected]'), | ||
] | ||
)); | ||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Learn More | ||
----------- | ||
|
||
To learn more about how to use the mailer component, refer to the :doc:`Symfony Framework Mailer documentation </mailer>`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.