-
-
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
Changes from 1 commit
e2c89df
bb4c064
6e59aa6
83477ab
9f81783
1b2b4d6
8a680bd
f17952c
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,8 +16,129 @@ Installation | |||||||
|
||||||||
.. include:: /components/require_autoload.rst.inc | ||||||||
|
||||||||
|
||||||||
Introduction | ||||||||
------------ | ||||||||
|
||||||||
|
||||||||
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
|
||||||||
use Symfony\Component\Mailer\Mailer; | ||||||||
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport; | ||||||||
|
||||||||
$transport = new SmtpTransport('localhost'); | ||||||||
$mailer = new Mailer($transport); | ||||||||
$mailer->send($email); | ||||||||
|
||||||||
Refer :doc:`Mime component </components/mime>` how to create `$email` object. | ||||||||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
|
||||||||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
Transport | ||||||||
--------- | ||||||||
|
||||||||
By default, the only transport available in the mailer component is Smtp. | ||||||||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
Below is the list of other popular providers with built in support. | ||||||||
- Amazon SES : symfony/amazon-mailer | ||||||||
wouterj marked this conversation as resolved.
Show resolved
Hide resolved
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
- Google Gmail : symfony/google-mailer | ||||||||
- Mandrill : symfony/mailchimp-mailer | ||||||||
- Mailgun : symfony/mailgun-mailer | ||||||||
- Postmark : symfony/postmark-mailer | ||||||||
- Sendgrid : symfony/sendgrid-mailer | ||||||||
|
||||||||
For example to use google's gmail as a transport you need to install symfony/google-mailer. | ||||||||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
.. code-block:: terminal | ||||||||
|
||||||||
$ composer require symfony/google-mailer | ||||||||
|
||||||||
.. include:: /components/require_autoload.rst.inc | ||||||||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
|
||||||||
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'); | ||||||||
$transport->send($email); | ||||||||
|
||||||||
Use a Dsn | ||||||||
--------- | ||||||||
|
||||||||
The mailer component provides a convenient way to create transport object from dsn string. | ||||||||
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.
Suggested change
|
||||||||
|
||||||||
use Symfony\Component\Mailer\Transport; | ||||||||
|
||||||||
$transport = Transport::fromDsn($dsn); | ||||||||
|
||||||||
Where `$dns` as one of the form below. | ||||||||
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. New line break after this |
||||||||
- smtp://user:pass@gmail | ||||||||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
- 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 dev to a "real" provider in production with same API. | ||||||||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
Failover transport | ||||||||
------------------ | ||||||||
|
||||||||
You can create failover transport with the help of `||` operator. | ||||||||
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.
Suggested change
|
||||||||
|
||||||||
Eg : | ||||||||
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. Remove this |
||||||||
|
||||||||
$dsn = 'api://id@postmark || smtp://key@sendgrid'; | ||||||||
|
||||||||
So if it fails at one transport, the mailer will attempt to send through the other transport. | ||||||||
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.
Suggested change
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. Did you missed the |
||||||||
|
||||||||
RoundRobin | ||||||||
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.
Suggested change
|
||||||||
---------- | ||||||||
|
||||||||
If you want to send mails via multiple transports, you can use the `&&` operator between the transports. | ||||||||
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.
Suggested change
|
||||||||
|
||||||||
Eg : | ||||||||
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. remove this 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. You mean the Eg: , right ? |
||||||||
|
||||||||
$dsn = 'api://id@postmark && smtp://key@sendgrid' | ||||||||
|
||||||||
|
||||||||
Async | ||||||||
----- | ||||||||
|
||||||||
If you want to use the async functionality you need to install `messenger` component. | ||||||||
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.
Suggested change
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. Even better, link to the docs of the component via |
||||||||
By default, `$bus` is null and if it is not configured, mailer is always using sync functionality. | ||||||||
And async when `$bus` is configured for EnvelopedMessage. | ||||||||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
.. code-block:: terminal | ||||||||
|
||||||||
$ composer require symfony/messenger | ||||||||
wouterj marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
.. include:: /components/require_autoload.rst.inc | ||||||||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
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 = 'smtp://null'; | ||||||||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
$transport = Transport::fromDsn($dsn); | ||||||||
$handler = new MessageHandler($transport); | ||||||||
|
||||||||
$bus = new MessageBus([ | ||||||||
new HandleMessageMiddleware(new HandlersLocator([ | ||||||||
SendEmailMessage::class => ['message_hander' => $handler], | ||||||||
harikt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
])), | ||||||||
]); | ||||||||
|
||||||||
$mailer = new Mailer($transport, $bus); | ||||||||
|
||||||||
$mailer->send($email, new SmtpEnvelope( | ||||||||
new Address('sender@example.com'), | ||||||||
[ | ||||||||
new Address('recepient@example.com'), | ||||||||
] | ||||||||
)); |
Uh oh!
There was an error while loading. Please reload this page.