Skip to main content
Resend Docs

Search documentation

Type to search this documentation.

On this pageOverview

Send emails with Symfony

Before you start, you'll need:

  1. Install

    Get the Resend Mailer Bridge package.

    Composer
    composer require symfony/resend-mailer

    If your application relies on Resend webhook events, also install the Symfony Webhook Component.

    Composer
    composer require symfony/webhook
  2. Configuring Mailer

    In your .env.local file, which you can create if needed, add the following:

    Shell
    MAILER_DSN=resend+api://API_KEY@default
    MAILER_RESEND_SECRET=SIGNING_SECRET

    Replace API_KEY with your Resend API key, and SIGNING_SECRET with your webhook secret, which can be retrieved from the Resend dashboard after creating a new webhook endpoint (see below).

  3. Send your first email

    In a controller, inject the Mailer:

    PHP
    public function __construct(
        private readonly MailerInterface $mailer,
    ) {
    }

    In a controller action, use the $this->mailer to send your email:

    PHP
    $this->mailer->send(
        (new Email())
            ->from('Acme <onboarding@resend.dev>')
            ->to('delivered@resend.dev')
            ->subject('Hello world')
            ->html('<strong>it works!</strong>')
    );

    Learn more about sending emails with Mailer Component in Symfony's documentation.

  4. Receive and handle webhooks

    Thanks to the Webhook Component, you can create a webhook listener.

    src/Webhook/ResendWebhookListener.php
    #[AsRemoteEventConsumer('mailer_resend')]
    readonly class ResendWebhookListener implements ConsumerInterface
    {
        public function __construct(
            #[Autowire(param: 'kernel.project_dir')] private string $projectDir,
        ) {
        }
    
        public function consume(RemoteEvent $event): void
        {
            if ($event instanceof MailerDeliveryEvent) {
                $this->handleMailDelivery($event);
            } elseif ($event instanceof MailerEngagementEvent) {
                $this->handleMailEngagement($event);
            } else {
                // This is not an email event
                return;
            }
        }
    
        private function handleMailDelivery(MailerDeliveryEvent $event): void
        {
            // Todo
        }
    
        private function handleMailEngagement(MailerEngagementEvent $event): void
        {
            // Todo
        }
    }

    Bind your listener to the Webhook routing config:

    config/packages/webhook.yaml
    framework:
      webhook:
        routing:
          mailer_resend:
            service: 'mailer.webhook.request_parser.resend'
            secret: '%env(MAILER_RESEND_SECRET)%'

    Next, register your application's webhook endpoint URL (example: https://{app_domain}/webhook/mailer_resend) in the Resend Dashboard:

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu