Guide · PHP
How to send email with Symfony
Configure Symfony Mailer with a Stack Emails SMTP DSN and send emails with the MailerInterface.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
In .env.local (URL-encode the key if it contains special characters):
MAILER_DSN=smtps://stackemails:se_live_xxxxxxxxxxxx@smtp.stackemails.com:465
2. Send your first email
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
public function signup(MailerInterface $mailer): Response
{
$email = (new Email())
->from("Acme <hello@mail.acme.com>")
->to("ada@example.com")
->subject("Welcome to Acme")
->html("<p>Thanks for signing up!</p>");
$mailer->send($email);
// ...
}
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.