Symfony Mailer

repository·8.2·Indexed 23 days ago

https://github.com/symfony/mailer

A high-level API for sending emails using various transport protocols such as SMTP. It supports fluent email definition via the Email class, Twig template rendering through symfony/twig-bridge, and includes a mailer:test command to verify transport configurations.

Tokens
1K
Snippets
3
Records
5
Agent score
32%

What's inside symfony/mailer

  1. Use Twig to render email templates

    8.2

    To use Twig templates for your email bodies, you must require symfony/twig-bridge. You need to set up a BodyRenderer with your Twig environment and register a MessageListener with an EventDispatcher. This listener ensures that TemplatedEmail objects are rendered correctly before being sent. When creating the transport and the mailer, pass the EventDispatcher to them.

    use Symfony\Bridge\Twig\Mime\BodyRenderer;
    use Symfony\Bridge\Twig\Mime\TemplatedEmail;
    use Symfony\Component\EventDispatcher\EventDispatcher;
    use Symfony\Component\Mailer\EventListener\MessageListener;
    use Symfony\Component\Mailer\Mailer;
    use Symfony\Component\Mailer\Transport;
    use Twig\Environment as TwigEnvironment;
    
    $twig = new TwigEnvironment(...);
    $messageListener = new MessageListener(null, new BodyRenderer($twig));
    
    $eventDispatcher = new EventDispatcher();
    $eventDispatcher->addSubscriber($messageListener);
    
    $transport = Transport::fromDsn('smtp://localhost', $eventDispatcher);
    $mailer = new Mailer($transport, null, $eventDispatcher);
    
    $email = (new TemplatedEmail())
        // ...
        ->htmlTemplate('emails/signup.html.twig')
        ->context([
            'expiration_date' => new \DateTimeImmutable('+7 days'),
            'username' => 'foo',
        ])
    ;
    $mailer->send($email);
  2. Send a basic email

    8.2

    You can send emails by creating a Transport from a DSN, initializing a Mailer instance, and using the Email class to define the message content. The Email object provides a fluent interface for setting the sender, recipients, subject, and body (both text and HTML).

    use Symfony\Component\Mailer\Transport;
    use Symfony\Component\Mailer\Mailer;
    use Symfony\Component\Mime\Email;
    
    $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);
  3. Test Mailer transports with mailer:test

    8.2

    The mailer:test command allows you to verify that a configured Mailer transport is working correctly by attempting to send a simple email message.

    Important Note: This command bypasses the Messenger bus if you have one configured in your application.