Symfony/Mailer for PHP: Complete Guide to Sending Email
\n\n1. What It Is and Why You Need It
\n\nSymfony/Mailer is a powerful and flexible component of the Symfony ecosystem, designed for sending emails from PHP applications. Unlike the built-in PHP mail() function, which requires manual configuration of headers and MIME types, Symfony/Mailer provides an object-oriented API for creating, formatting, and sending messages through various transports (SMTP, Sendmail, third-party service APIs).
The main task of the library is to abstract the email sending process, allowing the developer to focus on the content of the message rather than low-level protocol details. Symfony/Mailer supports HTML templates, attachments, image embedding, DKIM signing, and integration with popular email services via Mailer DSN (Data Source Name).
\n\nThe library is indispensable for projects requiring reliable sending of transactional emails (registration confirmation, password reset, notifications), marketing newsletters, or any other email notifications. Thanks to its modular architecture, Symfony/Mailer is easy to test (via MockTransport) and scale.
2. Installation
\n\nInstallation is done via Composer, the standard package manager for PHP. Run the following in your project root in the terminal:
\n\ncomposer require symfony/mailer\n\nIf you are using Symfony Flex (recommended), a configuration file config/packages/mailer.yaml will be automatically created after installation. To work with popular email services (SendGrid, Mailgun, Amazon SES, etc.), install the corresponding adapter, for example:
composer require symfony/sendgrid-mailer\ncomposer require symfony/mailgun-mailer\ncomposer require symfony/amazon-mailer\n\nYou will also need the symfony/http-client component for working with API transports:
composer require symfony/http-client\n\n3. Quick Start — Minimal Working Example
\n\nAfter installing the library, you can send your first email in just a few lines. Create a file send_email.php:
<?php\n// send_email.php\nrequire_once 'vendor/autoload.php';\n\nuse Symfony\\Component\\Mailer\\Mailer;\nuse Symfony\\Component\\Mailer\\Transport\\Smtp\\EsmtpTransport;\nuse Symfony\\Component\\Mime\\Email;\n\n// 1. Create transport (Gmail SMTP server)\n$transport = new EsmtpTransport('smtp.gmail.com', 587, true);\n$transport->setUsername('your-email@gmail.com');\n$transport->setPassword('your-app-password'); // Use an app password\n\n// 2. Create Mailer\n$mailer = new Mailer($transport);\n\n// 3. Create email\n$email = (new Email())\n ->from('your-email@gmail.com')\n ->to('recipient@example.com')\n ->subject('Hello from Symfony Mailer!')\n ->text('This is a test email sent using Symfony/Mailer.')\n ->html('<h1>Hello!</h1><p>This is the <strong>HTML</strong> version of the email.</p>');\n\n// 4. Send\ntry {\n $mailer->send($email);\n echo "Email sent successfully!\\n";\n} catch (\\Exception $e) {\n echo "Error: " . $e->getMessage() . "\\n";\n}\n\nRun the script:
\n\nphp send_email.php\n\nIf everything is configured correctly, you will receive the email at the specified address.
\n\n4. Main Methods/Classes
\n\n4.1. Email::class — Class for creating an email
\nSignature: new Email() (constructor without parameters)
What it does: Creates an email object with setter methods for configuring all attributes (sender, recipient, subject, body, attachments, etc.)