Feb 1, 2023 by Zacharia Mansouri | 1862 views
With the release of Laravel 9, the Swift Mailer (that is no longer maintained) has been replaced by the Symfony Mailer. You can already find some useful information about this change along all the other ones in the Upgrade Guide from Laravel 8.x to 9.0. However this guide does not contain enough information if you want to send fully customized emails. This blog post proposes you a solution coming directly from the Symfony documentation!
In this blog post, the following email parameters are customizable:
$senderPorte.g. 587 (SMTP encrypted communications)
$senderUsernameusually the same as
$senderEmailor any username used when registering the e-mail address
$senderNamee.g. My Company Name
$emailSubjecte.g. Registration Link
$emailBodythe (HTML) body composing the content of the email
As stated in the Upgrade Guide, you must install some new dependencies:
composer require symfony/mailgun-mailer symfony/http-client composer require symfony/postmark-mailer symfony/http-client
Don't hesitate to consult the guide first, as you can consider this blog post as some kind of complement for the specific case of fully customizable emails.
The changes between the ways
$mailer are created will be explained below the following pieces of code.
// Message $message = (new \Swift_Message($emailSubject)) ->setFrom($senderEmail, $senderName) ->setTo($receiverEmail) ->setBody($emailBody, 'text/html'); // Transport $transport = (new \Swift_SmtpTransport($senderHost, $senderPort, $senderEncryption)) ->setUsername($senderUsername) ->setPassword($senderPassword); // Mailer $mailer = new \Swift_Mailer($transport); $mailer->send($message);
// New imports use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Address; use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mailer\Transport; // Message $message = (new Email()) ->subject($emailSubject) ->from(new Address($senderEmail, $senderName)) ->to($receiverEmail) ->html($emailBody); // Transport $transport = Transport::fromDsn( 'smtp://' . $senderUsername . ':' . $senderPassword . '@' . $senderHost . ':' . $senderPort ); // Mailer $mailer = new Mailer($transport); $mailer->send($message);
The following classes must be imported:
use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Address; use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mailer\Transport;
Instead of building a
$message object by instantiating a
\Swift_Message object with the
$emailSubject and calling the
setBody methods on it, we must now create an
html. Note that:
Addressobject with the
$senderNameis instantiated in the
textmethod instead of the
Instead of building a
$transport object by instanciating a
\Swift_SmtpTransport object with the
$senderEncryption and calling the
setPassword methods on it, we must now create the
$transport object by calling the
fromDsn method statically from the
Transport class and passing an SMTP URL to it. Here is how that URL looks like if we use the preliminary information written at the beginning of this blog post:
Symfony gives you a direct access to the SMTP URL instead of providing you with methods to build it like Swift did.
$senderEncryption does not appear in the new piece of code. As stated in the Symfony documentation, by default, SMTP transports perform TLS peer verification. If needed, you can check that documentation to learn how to disable it (pro-tip: just append
?verify_peer=0 to the URL, but it is not recommended).
The only change is that the
$mailer object is created by instantiating a
Mailer object instead of the old
By combining the information from the Upgrade Guide, the Symfony documentation and this blog post, you are now able to have a total control on the emails you want to send, no matter how many sending email addresses you want to use!