WEBVTT NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com 00:00:01.096 --> 00:00:05.836 align:middle We've already learned quite a bit about how to customize a specific email... 00:00:06.066 --> 00:00:07.346 align:middle with a lot more coming. 00:00:07.746 --> 00:00:10.906 align:middle But how do we customize how an email is sent. 00:00:11.696 --> 00:00:16.766 align:middle In Symfony, the way that your messages are delivered is called a transport. 00:00:17.506 --> 00:00:22.806 align:middle Go back to your terminal and run: git status When we installed the Mailer component, 00:00:23.016 --> 00:00:26.286 align:middle its recipe did a couple of interesting things. 00:00:26.286 --> 00:00:31.786 align:middle First, it created a new file called config/packages/mailer.yaml. 00:00:32.366 --> 00:00:33.636 align:middle Let's open up that up. 00:00:36.406 --> 00:00:41.626 align:middle Wow... as you can see: the mailer system doesn't really have a lot of config. 00:00:42.206 --> 00:00:47.846 align:middle The only thing here is the dsn: a URL that tells Mailer what server 00:00:47.996 --> 00:00:51.316 align:middle or cloud service to use for delivery. 00:00:52.056 --> 00:00:55.696 align:middle This references an environment variable called MAILER_DSN. 00:00:55.696 --> 00:01:02.426 align:middle Hey! That's the error we just saw: Environment variable not found: "MAILER_DSN". 00:01:03.386 --> 00:01:06.216 align:middle The recipe also modified the .env file. 00:01:06.806 --> 00:01:10.206 align:middle If you run git diff .env Yep! 00:01:10.566 --> 00:01:15.116 align:middle You'll see that it added a section with an example MAILER_DSN. 00:01:16.116 --> 00:01:16.966 align:middle Open up .env. 00:01:16.966 --> 00:01:23.066 align:middle And, at the bottom, uncomment that MAILER_DSN line. 00:01:24.216 --> 00:01:29.006 align:middle By default, this tries to send to a local SMTP server... 00:01:29.206 --> 00:01:32.826 align:middle and I definitely do not have one of those running. 00:01:33.366 --> 00:01:35.446 align:middle But... let's try it anyways. 00:01:36.176 --> 00:01:39.216 align:middle Refresh to resubmit the registration form and... 00:01:39.706 --> 00:01:46.166 align:middle boom! Connection could not be established with host "tcp://localhost:25" 00:01:47.216 --> 00:01:49.256 align:middle So how are we going to send emails? 00:01:49.526 --> 00:01:49.896 align:middle Because... 00:01:49.896 --> 00:01:52.426 align:middle there are a lot of different options. 00:01:53.036 --> 00:01:55.376 align:middle You could run your own SMTP server... 00:01:55.456 --> 00:01:57.666 align:middle which is not something I recommend... 00:01:58.086 --> 00:02:02.356 align:middle or register with a cloud email sender - like SendGrid - 00:02:02.686 --> 00:02:06.366 align:middle and use your connection details from them for Mailer. 00:02:07.116 --> 00:02:11.356 align:middle Mailer supports a bunch of the most famous cloud providers... 00:02:11.356 --> 00:02:15.296 align:middle as well as any cloud provider that implements SMTP... 00:02:15.446 --> 00:02:16.436 align:middle which is like... 00:02:16.636 --> 00:02:17.746 align:middle all of them. 00:02:17.746 --> 00:02:20.166 align:middle We're going to show how to use SendGrid a bit later. 00:02:21.006 --> 00:02:24.376 align:middle Why are we not going to use SendGrid right now? 00:02:24.876 --> 00:02:25.266 align:middle Because... 00:02:25.506 --> 00:02:29.976 align:middle when you're developing and debugging your emails, there's a better option. 00:02:29.976 --> 00:02:37.146 align:middle Instead of sending real emails to a real email server, you can send them to a "fake" mailbox. 00:02:37.756 --> 00:02:41.496 align:middle One of the most famous tools to do this is called MailCatcher. 00:02:42.436 --> 00:02:45.816 align:middle Basically, you download MailCatcher, start it on your machine, 00:02:46.056 --> 00:02:50.846 align:middle and it creates a temporary SMTP server that you can send to. 00:02:51.386 --> 00:02:55.286 align:middle But instead of delivering the messages, it holds onto them 00:02:55.516 --> 00:02:59.016 align:middle and you can view them all in a fake inbox in your browser. 00:02:59.636 --> 00:03:04.796 align:middle MailCatcher is written in Ruby and a similar tool - MailHog - is written in Go. 00:03:05.316 --> 00:03:07.536 align:middle Those are both great options. 00:03:08.146 --> 00:03:11.506 align:middle But... to save me the headache of getting those running, 00:03:11.806 --> 00:03:15.236 align:middle I'm going to use a third option called MailTrap. 00:03:16.046 --> 00:03:18.306 align:middle Head to mailtrap.io. 00:03:19.146 --> 00:03:24.576 align:middle This is basically a "hosted" version of those tools: it gives us a fake SMTP server 00:03:24.726 --> 00:03:28.616 align:middle and fake inbox, but we don't need to install anything. 00:03:29.186 --> 00:03:31.856 align:middle And it has an excellent free plan. 00:03:31.856 --> 00:03:37.376 align:middle After you register, you'll end up in a spot like this: with a "Demo inbox". 00:03:38.116 --> 00:03:39.646 align:middle Click into that Demo inbox. 00:03:39.646 --> 00:03:45.906 align:middle On the right, you'll see a bunch of information about how to connect to this. 00:03:46.836 --> 00:03:51.806 align:middle At the time of recording, they do have specific instructions for Symfony 4... 00:03:52.056 --> 00:03:56.786 align:middle but these are for using MailTrap with SwiftMailer, not Symfony Mailer. 00:03:57.846 --> 00:04:00.386 align:middle No worries, setup is dead simple. 00:04:01.006 --> 00:04:07.626 align:middle The DSN follows a standard structure: username:password@server:port. 00:04:08.816 --> 00:04:18.236 align:middle Copy the username from MailTrap, paste, add a colon, copy and paste the password, 00:04:20.436 --> 00:04:28.036 align:middle then @ the server - smtp.mailtrap.io - one more colon, and the port. 00:04:29.296 --> 00:04:30.606 align:middle We could use any of these. 00:04:31.066 --> 00:04:35.196 align:middle Try 2525. Done! 00:04:35.766 --> 00:04:41.326 align:middle If we haven't messed anything up, our email should be delivered to our MailTrap inbox. 00:04:42.146 --> 00:04:42.836 align:middle Let's try it! 00:04:43.676 --> 00:04:46.136 align:middle Refresh the form submit and... 00:04:46.566 --> 00:04:48.586 align:middle ah! Validation error. 00:04:49.246 --> 00:04:55.086 align:middle The last time we tried this, the email failed to send but the user was saved to the database. 00:04:55.886 --> 00:04:58.056 align:middle Make the email unique by adding a "2". 00:04:58.576 --> 00:05:01.806 align:middle Then click the terms, enter any password and... 00:05:02.126 --> 00:05:05.766 align:middle register! Ok, no errors! 00:05:06.046 --> 00:05:07.156 align:middle Go check MailTrap! 00:05:08.166 --> 00:05:08.856 align:middle There it is! 00:05:08.906 --> 00:05:15.166 align:middle It's got the subject, text content, but no HTML content because we haven't set that yet. 00:05:16.326 --> 00:05:19.536 align:middle There are also a couple of other cool debugging features in MailTrap - 00:05:19.976 --> 00:05:21.486 align:middle we'll talk about some of these soon. 00:05:21.486 --> 00:05:27.986 align:middle Now that we've got some success, it's time to attack the obvious shortcoming of this email... 00:05:28.336 --> 00:05:29.746 align:middle it's just text! 00:05:30.046 --> 00:05:35.426 align:middle It's not 1995 anymore people, we need to send HTML emails. 00:05:35.716 --> 00:05:41.016 align:middle And Mailer gives us a great way to do this: native integration with Twig. 00:05:41.646 --> 00:05:42.756 align:middle That's next.