Great DX - Great Perf - Great Design (auto) proxy configuration DNS cache pre-population 1st class error handling auto-gzip compression timeout management public key pinning progress callback extended info request abort HTTP/2-push IDN support Psr18Client time stats streaming base URI ...
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\Renderer; 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 Renderer($twig); echo $renderer->render($email)->toString(); Native integration with Twig