Symfony/Mailer is a powerful email solution for PHP that simplifies email handling. This Mailer component aims to make sending emails easier and more efficient. It helps build clean, re-usable and testable emails, while handling the low-level infrastructure parts for you.
To use Symfony/Mailer, first, you need to install it via Composer using the command $ composer require symfony/mailer
. Once installed, you can use it in your application.
Here is an example of creating a new email and sending it:
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
$transport = Transport::fromDsn('smtp://localhost');
$mailer = new Mailer($transport);
$email = (new Email())
->from('hello@example.com')
->to('you@example.com')
->subject('Time for Symfony Mailer!')
->text('Sending emails is fun again!')
->html('<p>See Twig integration for better HTML integration!</p>');
$mailer->send($email);
In this example, an SMTP server is used as a transport layer to actually send the emails. The Mailer object is then initiated to work with this transport layer. An email object is created with sender, recipient, subject, text and HTML content. Finally, this email is sent through the Mailer.
If you want to use Twig integration you need to install symfony/twig-bridge
, after that, you can use it in the way that's presented in the readme.
Symfony/Mailer's documentation can be found on the Symfony website. You can access it by visiting the Symfony Mailer Documentation. You will find there more in-depth information about the usage, configuration and extensive capabilities of the Symfony/Mailer component.