Skip to content

Commit 404f397

Browse files
committed
minor #37542 [Mailer] Updated README for the Mailer component (wouterj)
This PR was merged into the 4.4 branch. Discussion ---------- [Mailer] Updated README for the Mailer component | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs#13945 I think it's worth adding a second example showcasing the twig integration for 2 reasons: * If you're used to use this component in the framework, I think it's likely you want Twig integration in standalone apps. * The integration actually lives in `symfony/twig-bridge`, it won't be very easy to catch while reading the component code - unless you're very comfortable with the Symfony architecture. Commits ------- 696560c690 Updated README for the Mailer component
2 parents 8704c18 + 653a1c9 commit 404f397

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,66 @@ Mailer Component
33

44
The Mailer component helps sending emails.
55

6+
Getting Started
7+
---------------
8+
9+
```
10+
$ composer require symfony/mailer
11+
```
12+
13+
```php
14+
use Symfony\Component\Mailer\Transport;
15+
use Symfony\Component\Mailer\Mailer;
16+
17+
$transport = Transport::fromDsn('sendgrid://KEY@default');
18+
$mailer = new Mailer($transport);
19+
20+
$email = (new Email())
21+
22+
23+
24+
//->bcc('[email protected]')
25+
//->replyTo('[email protected]')
26+
//->priority(Email::PRIORITY_HIGH)
27+
->subject('Time for Symfony Mailer!')
28+
->text('Sending emails is fun again!')
29+
->html('<p>See Twig integration for better HTML integration!</p>');
30+
31+
$mailer->send($email);
32+
```
33+
34+
To enable the Twig integration of the Mailer, require `symfony/twig-bridge` and
35+
set up the `BodyRenderer`:
36+
37+
```php
38+
use Symfony\Bridge\Twig\Mime\BodyRenderer;
39+
use Symfony\Component\EventDispatcher\EventDispatcher;
40+
use Symfony\Component\Mailer\EventListener\MessageListener;
41+
use Twig\Environment as TwigEnvironment;
42+
43+
$twig = new TwigEnvironment(...);
44+
$messageListener = new MessageListener(new BodyRenderer($twig));
45+
46+
$eventDispatcher = new EventDispatcher();
47+
$eventDispatcher->addSubscriber($messageListener);
48+
49+
$mailer = new Mailer($transport, null, $eventDispatcher);
50+
51+
$email = (new TemplatedEmail())
52+
// ...
53+
->htmlTemplate('emails/signup.html.twig')
54+
->context([
55+
'expiration_date' => new \DateTime('+7 days'),
56+
'username' => 'foo',
57+
])
58+
;
59+
$mailer->mail($email);
60+
```
61+
662
Resources
763
---------
864

65+
* [Documentation](https://symfony.com/doc/current/mailer.html)
966
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
1067
* [Report issues](https://github.com/symfony/symfony/issues) and
1168
[send Pull Requests](https://github.com/symfony/symfony/pulls)

0 commit comments

Comments
 (0)