README.md
1Mailer Component
2================
3
4The Mailer component helps sending emails.
5
6Getting Started
7---------------
8
9```
10$ composer require symfony/mailer
11```
12
13```php
14use Symfony\Component\Mailer\Transport;
15use Symfony\Component\Mailer\Mailer;
16
17$transport = Transport::fromDsn('smtp://localhost');
18$mailer = new Mailer($transport);
19
20$email = (new Email())
21 ->from('hello@example.com')
22 ->to('you@example.com')
23 //->cc('cc@example.com')
24 //->bcc('bcc@example.com')
25 //->replyTo('fabien@example.com')
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
34To enable the Twig integration of the Mailer, require `symfony/twig-bridge` and
35set up the `BodyRenderer`:
36
37```php
38use Symfony\Bridge\Twig\Mime\BodyRenderer;
39use Symfony\Bridge\Twig\Mime\TemplatedEmail;
40use Symfony\Component\EventDispatcher\EventDispatcher;
41use Symfony\Component\Mailer\EventListener\MessageListener;
42use Symfony\Component\Mailer\Mailer;
43use Symfony\Component\Mailer\Transport;
44use Twig\Environment as TwigEnvironment;
45
46$twig = new TwigEnvironment(...);
47$messageListener = new MessageListener(null, new BodyRenderer($twig));
48
49$eventDispatcher = new EventDispatcher();
50$eventDispatcher->addSubscriber($messageListener);
51
52$transport = Transport::fromDsn('smtp://localhost', $eventDispatcher);
53$mailer = new Mailer($transport, null, $eventDispatcher);
54
55$email = (new TemplatedEmail())
56 // ...
57 ->htmlTemplate('emails/signup.html.twig')
58 ->context([
59 'expiration_date' => new \DateTime('+7 days'),
60 'username' => 'foo',
61 ])
62;
63$mailer->send($email);
64```
65
66Resources
67---------
68
69 * [Documentation](https://symfony.com/doc/current/mailer.html)
70 * [Contributing](https://symfony.com/doc/current/contributing/index.html)
71 * [Report issues](https://github.com/symfony/symfony/issues) and
72 [send Pull Requests](https://github.com/symfony/symfony/pulls)
73 in the [main Symfony repository](https://github.com/symfony/symfony)
74