Hello Symfony Mailer

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

The year is 1995: internet connection speeds are reaching a blistering 56 kbit/s, GeoCities is transforming everyone into an accomplished web designer, and sending emails... is all the rage.

Quick, fast-forward 25 years! Self-driving cars are a reality, you can download an entire HD movie in seconds, we can send rockets into space and then land them safely back on Earth and... yes, love it or hate it... sending emails is still all the rage... or at least... something nobody can avoid.

Yep, emails are still a huge part of our life and pretty much every app needs to send at least some... if not a lot of emails. But sending emails has always been kind of a pain - it feels like an old process. On top of that, emails are hard to preview, a pain to debug, there are multiple ways to deliver them - do I need an SMTP server? - each email has text and HTML parts, and don't even get me started about styling emails and embedding CSS in a way that will work in all mail clients. Oof.

But then, out of the ashes of this ancient practice grew... a hero. Ok it's actually just a Symfony component - but a cool one! Enter Symfony Mailer: a fresh & modern library that makes something old - sending emails - feel... new! Seriously, Mailer actually makes sending emails fun again and handles the ugliest details automatically. Will you love sending emails after this tutorial? Yea... I think you kinda might!

Setting up the App

As always, unless you're just "mailing it in", you should totally code along with me. Dial onto the internet, download the course code from this page and unzip it with WinRAR 1.54b. Inside, you'll find a start/ directory with the same code that you see here. Open up the README.md file to find all the setup details. The last step will be to open a terminal, move into the project and use the Symfony Binary to start a web server:

symfony serve

If you don't have the Symfony binary, you can grab it at Symfony.com/download. Once that's running, open your favorite browser - mine is Netscape Navigator - and go to https://localhost:8000 to see... The Space Bar! A news site for aliens... and the app that you probably recognize from other Symfony 4 tutorials here on the site.

In this tutorial, we'll be using Symfony 4.3. There are a few cool features that are coming in Symfony 4.4 and 5.0... but don't worry! I'll point those out along the way: they aren't big changes, mostly some nice debugging features.

Installing Mailer

Like most things in Symfony, the Mailer component is not installed by default. No problem, find your terminal, open a new tab and run:

composer require symfony/mailer

Notice that I didn't just use composer require mailer... using the "mailer" alias. Remember: Symfony Flex lets us say things like composer require forms or composer require templating and then it maps that to a recommended package. But at the time of this recording, composer require mailer would not download the Mailer component. Nope, it would download Swift Mailer... was was the recommended library for sending emails with Symfony before Symfony 4.3: that's when the Mailer component was introduced.

And even when you're Googling for documentation about Symfony's Mailer, be careful: you might end up on the docs for using SwiftMailer inside Symfony. The Mailer docs might be the second or third result.

Anyways after this installs, yea! We get some nice, post-install instructions. We'll talk about all of this.

The first step... is to create and configure an Email object! Let's do that next... then send it!

Leave a comment!

This tutorial is built on Symfony 4.3, but will work well with Symfony 4.4 or 5.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "aws/aws-sdk-php": "^3.87", // 3.110.11
        "knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
        "knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
        "knplabs/knp-snappy-bundle": "^1.6", // v1.6.0
        "knplabs/knp-time-bundle": "^1.8", // v1.9.1
        "league/flysystem-aws-s3-v3": "^1.0", // 1.0.23
        "league/flysystem-cached-adapter": "^1.0", // 1.0.9
        "league/html-to-markdown": "^4.8", // 4.8.2
        "liip/imagine-bundle": "^2.1", // 2.1.0
        "nexylan/slack-bundle": "^2.1,<2.2.0", // v2.1.0
        "oneup/flysystem-bundle": "^3.0", // 3.1.0
        "php-http/guzzle6-adapter": "^1.1", // v1.1.1
        "sensio/framework-extra-bundle": "^5.1", // v5.4.1
        "stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
        "symfony/asset": "^4.0", // v4.3.4
        "symfony/console": "^4.0", // v4.3.4
        "symfony/flex": "^1.0", // v1.6.2
        "symfony/form": "^4.0", // v4.3.4
        "symfony/framework-bundle": "^4.0", // v4.3.4
        "symfony/mailer": "4.3.*", // v4.3.4
        "symfony/messenger": "4.3.*", // v4.3.4
        "symfony/orm-pack": "^1.0", // v1.0.6
        "symfony/security-bundle": "^4.0", // v4.3.4
        "symfony/sendgrid-mailer": "4.3.*", // v4.3.4
        "symfony/serializer-pack": "^1.0", // v1.0.2
        "symfony/twig-bundle": "^4.0", // v4.3.4
        "symfony/twig-pack": "^1.0", // v1.0.0
        "symfony/validator": "^4.0", // v4.3.4
        "symfony/web-server-bundle": "^4.0", // v4.3.4
        "symfony/webpack-encore-bundle": "^1.4", // v1.6.2
        "symfony/yaml": "^4.0", // v4.3.4
        "twig/cssinliner-extra": "^2.12", // v2.12.0
        "twig/extensions": "^1.5", // v1.5.4
        "twig/inky-extra": "^2.12" // v2.12.0
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.0", // 3.2.2
        "easycorp/easy-log-handler": "^1.0.2", // v1.0.7
        "fzaninotto/faker": "^1.7", // v1.8.0
        "symfony/browser-kit": "4.3.*", // v4.3.5
        "symfony/debug-bundle": "^3.3|^4.0", // v4.3.4
        "symfony/dotenv": "^4.0", // v4.3.4
        "symfony/maker-bundle": "^1.0", // v1.13.0
        "symfony/monolog-bundle": "^3.0", // v3.4.0
        "symfony/phpunit-bridge": "^3.3|^4.0", // v4.3.4
        "symfony/profiler-pack": "^1.0", // v1.0.4
        "symfony/var-dumper": "^3.3|^4.0" // v4.3.4
    }
}