ba98fd74d6
This PR was squashed before being merged into the 5.2-dev branch.
Discussion
----------
[Mailer] Implement additional mailer transport options
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix #37300
| License | MIT
| Doc PR | https://github.com/symfony/symfony-docs/pull/13911
This implements additional transport configuration options mentioned in #37300. It also adds a `command` option to be able to define the command used by the `sendmail` transport.
Examples:
```yml
framework:
mailer:
transports:
sendmail: sendmail://default?command=/usr/sbin/sendmail%%20-oi%%20-t
local_domain: smtps://smtp.example.com?local_domain=example.org
restart_threshold: smtps://smtp.example.com?restart_threshold=10&restart_threshold_sleep=1
ping_threshold: smtps://smtp.example.com?ping_threshold=200
```
Commits
-------
|
||
---|---|---|
.. | ||
Bridge | ||
DataCollector | ||
Event | ||
EventListener | ||
Exception | ||
Header | ||
Messenger | ||
Test | ||
Tests | ||
Transport | ||
.gitattributes | ||
.gitignore | ||
CHANGELOG.md | ||
composer.json | ||
DelayedEnvelope.php | ||
Envelope.php | ||
LICENSE | ||
Mailer.php | ||
MailerInterface.php | ||
phpunit.xml.dist | ||
README.md | ||
SentMessage.php | ||
Transport.php |
Mailer Component
The Mailer component helps sending emails.
Getting Started
$ composer require symfony/mailer
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
$transport = Transport::fromDsn('smtp://localhost');
$mailer = new Mailer($transport);
$email = (new Email())
->from('hello@example.com')
->to('you@example.com')
//->cc('cc@example.com')
//->bcc('bcc@example.com')
//->replyTo('fabien@example.com')
//->priority(Email::PRIORITY_HIGH)
->subject('Time for Symfony Mailer!')
->text('Sending emails is fun again!')
->html('<p>See Twig integration for better HTML integration!</p>');
$mailer->send($email);
To enable the Twig integration of the Mailer, require symfony/twig-bridge
and
set up the BodyRenderer
:
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Mailer\EventListener\MessageListener;
use Twig\Environment as TwigEnvironment;
$twig = new TwigEnvironment(...);
$messageListener = new MessageListener(new BodyRenderer($twig));
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber($messageListener);
$mailer = new Mailer($transport, null, $eventDispatcher);
$email = (new TemplatedEmail())
// ...
->htmlTemplate('emails/signup.html.twig')
->context([
'expiration_date' => new \DateTime('+7 days'),
'username' => 'foo',
])
;
$mailer->mail($email);