Send emails with Symfony
Prerequisites
Section titled “Prerequisites”Before you start, you'll need:
- A Resend API key
- A verified domain
Install
Get the Resend Mailer Bridge package.
Composer composer require symfony/resend-mailerIf your application relies on Resend webhook events, also install the Symfony Webhook Component.
Composer composer require symfony/webhookConfiguring Mailer
In your
.env.localfile, which you can create if needed, add the following:Shell MAILER_DSN=resend+api://API_KEY@default MAILER_RESEND_SECRET=SIGNING_SECRETReplace
API_KEYwith your Resend API key, andSIGNING_SECRETwith your webhook secret, which can be retrieved from the Resend dashboard after creating a new webhook endpoint (see below).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->mailerto 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.
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:
Examples
Section titled “Examples”Symfony App
Full Symfony web application
Basic Send
Basic, batch, and prevent-threading send
Attachments
File attachments and inline images (CID)
Scheduling
Schedule emails for future delivery
Templates
Send emails using Resend hosted templates
Double Opt-in
Double opt-in subscription flow
Inbound Webhooks
Receive and process inbound emails
Audiences
Manage contacts and audiences
Domains
Create and manage sending domains