Mailer

MAILER_DSN

The delivery setting lives in the environment file under the name MAILER_DSN. It tells Symfony whether to use a local sendmail binary, an external SMTP server, or a test catcher.

Use the native sender on a laptop and a real provider in production. Keep passwords out of version control by storing them in local environment overrides.

MAILER_DSN=native://default

Build an Email

The Email object holds sender, recipient, subject, and body in one fluent chain. Each call returns the same object so calls can be linked in a readable line.

Always set a sender and at least one recipient before sending. Prefer plain text for system messages and add HTML only when the design truly needs it.

use Symfony\Component\Mime\Email; $email = (new Email())->from("hello@example.com")->to("user@example.com")->subject("Welcome")->text("Hello and welcome"); echo $email->getSubject();

Send it

Sending is one call on the mailer service with the prepared message object. Inject the mailer interface into a service and pass the address as a typed argument.

Build the message first and send it last so validation errors never trigger partial delivery. Log the recipient after success to simplify support and audits.

namespace App\Service; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Email; class SignupService { public function __construct(private MailerInterface $mailer) { } public function sendWelcome(string $address): void { $email = (new Email())->from("hello@example.com")->to($address)->subject("Welcome")->text("Hello and welcome"); $this->mailer->send($email); } }

Test with mailer:test

The test command sends a probe message through the configured transport without changing application code. It confirms credentials, network access, and sender permissions in one step.

Run it after every mail setting change and check the inbox or the provider log. Fix the DSN first when delivery fails, then review message headers.

php bin/console mailer:test user@example.com

mail sent to user@example.com

Next: Cache

Article author: Arthur Isaev