use Symfony\Component\Mime\Email; $email = (new Email()) ->from('[email protected]') ->to('[email protected]') ->subject('Some subject') ->text('Some text message') ->html('Some HTML message') ->attach('doc.txt') ; The basics
$email = (new Email()) ->from('[email protected]') ->to('[email protected]') ->subject('Some subject') ->text('Some text message') ->html('Some HTML message') ; echo strlen(serialize($email)); Email is a data object Only 2k vs 16k for Swiftmailer
$email = (new Email()) ->text('Some text message') ->html('Some HTML message') ; $email = (new \Swift_Message()) ->setBody('Some text message') ->addPart('Some HTML message', 'text/html') ; A better data object model 16k serialized 38 objects complex serialization "fixed" headers 2k serialized 7 objects simple serialization "dynamic" headers
echo $email->toString(); sleep(2); $email->to('[email protected]'); echo $email->toString(); Fixed vs dynamic headers Different set of headers Date, Boundary, Message-ID
// Email extends Message use Symfony\Component\Mime\Message; use Symfony\Component\Mime\Part\TextPart; use Symfony\Component\Mime\Header\Headers; $body = new TextPart('Some content'); $headers = (new Headers()) ->addMailboxListHeader('From', ['[email protected]']) ->addMailboxListHeader('To', ['[email protected]']) ->AddTextHeader('Subject', 'Some subject') ; $email = new Message($headers, $body); And you get full control!
$txt = new TextPart('Some content'); $html = new TextPart('Some content', 'html'); $body = new AlternativePart($txt, $html); $email = new Message($headers, $body); Get creative
// Email extends Message extends RawMessage use Symfony\Component\Mime\RawMessage; $message = new RawMessage($email->toString()); $message->toString(); Go raw! Serialize an email as a string instead of a PHP object
use Symfony\Bridge\Twig\Mime\BodyRenderer; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Twig\Environment; use Twig\Loader\FilesystemLoader; use Symfony\Component\Mime\NamedAddress; $twig = new Environment($loader = new FilesystemLoader(__DIR__.'/templates')); $loader->addPath(__DIR__.'/images', 'images'); $email = (new TemplatedEmail()) ->from('[email protected]') ->to(new NamedAddress('[email protected]', 'Fabien')) ->text('Some text content') ->htmlTemplate('simple.html.twig') ->context([ 'city' => 'Lille' ]) ; $renderer = new BodyRenderer($twig); $renderer->render($email); echo $email->toString(); Native integration with Twig
Unified behaviour across all providers Same events for all transports Unified DSNs / one env var MAILER_DSN Easily switch from SMTP in dev to a "real" provider in prod / same API Same message interface and serialization
SMTP / HTTP / API? SMTP HTTP API Offline Yes via Mailcatcher No / Mock No / Mock Mailcatcher Yes No No Standard Yes No No Symfony generated Yes Yes No Fast No Yes Yes
public function email(TransportInterface $transport) { $email = (new Email())->…; $transport->send($email); return new Response('Sent'); } Sending emails
$mailer = new Mailer($transport, $bus); Sync if $bus is null OR bus not configured Async is $bus is configured for EnvelopedMessage Sync or Async, your choice
public function email(MailerInterface $mailer) { $email = (new Email())->…; $mailer->send($email); return new Response('Sent... eventually'); } Sync or Async, your choice
public function email(MailerInterface $mailer) { $email = (new Email())->…; $mailer->send($email, new SmtpEnvelope(...)); return new Response('Sent... eventually'); } Sync or Async, your choice
Emails are sent async via AMQP framework: messenger: routing: 'Symfony\Component\Mailer\EnvelopedMessage': amqp Emails are sent immediately framework: messenger: routing: #'Symfony\Component\Mailer\EnvelopedMessage': amqp Sync or Async, your choice
class MessageHandler { private $transport; public function __construct(TransportInterface $transport) { $this->transport = $transport; } public function __invoke(EnvelopedMessage $message) { $this->transport->send($message->getMessage(), $message->getEnvelope()); } } public="false">
Leverages the Symfony ecosystem Twig Encore Serializer Messenger PSR logger CSS selector Event Dispatcher Dependency Injection Container Symfony Polyfills (punycode, intl, …) … with all the great features you love in Swiftmailer 6 Symfony Mailer