Symfony Mailer: How to Create Emails | Templated Email | Mail Catcher - [Symfony Workshop 2021]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this one i'm going to show you how to create and send emails in symphony using the mailer we'll start with something fairly static and basic and then we'll go for something a bit more complex where we pass in variables make the email dynamic we'll also style it so it's a bit prettier and i'll also show you how to add attachments but just a quick reminder before that i record in high resolution so there's no need to watch on a blurry screen and also if it's your first time to my channel and you'd like youtube to show you more of my stuff all you need to do is subscribe and click the notification icon i'll start by creating a new symphony project like always so symphony new sending emails is a project directory that i'm going to use and i'm just specifying version 5.1 if you do the same then you'll know that everything works exactly the same as mine so i'll move into that location now and we're going to need a couple of things to get going so first things first we'll get mailer so that's the symfony mail package and that's what we're going to use to create and send our emails with and then we also need to get annotations because we're going to use that for routing because i'm going to trigger everything from a controller and then i'm going to do is just get rid of this get repository symphony installs git repository by default when you create projects using the binary but i don't need to create a git repo okay over to phpstorm where i've opened up the project and i'm just going to create controller called email controller and i'll file everything from this so email controller extends abstract controller and we just need one simple method in which we'll create emails and fire them off so we'll just call this send email and i'll just pass one argument which is mailer interface and that'll give us our mailer which is what we actually use to send the emails but before we send one we've got to actually create one so i'll do that now so create an email variable and that will hold an email object now we're just going to set various things on this like to subject from etc so first off i'll do from and we'll just make up an application email sales app dot com then we'll add a two address we'll just use some example address so email example.com will do us next i'll add a subject i recommend always having a subject otherwise your emails can look spammy and they might not get delivered so always make sure you've got a subject in there and then i'm just gonna add the content of the email as in the body content so we'll just put something simple thank you your order has been placed because this is just to uh try it out and see if it's working and so in order to dispatch this email this is where we use our mailer and it's just mailer send and then you pass in the email object it's as simple as that we'll put a bit of feedback to view in our browser so we know that things have gone without hitch no exceptions have been thrown now normally to send an email you need to set up a transport so you can set up your own smtp server or you can use a third-party transport i'm actually going to do neither of those because i want this to be a recording about development and rather than one about configuration so i'm going to set up a mail catcher which will mimic the sending of the email and i'll be able to view the email in a browser the way i'm going to set up my mail capture is using docker and docker compose don't be intimidated this is going to be really simple all you need to do is have docker desktop installed and just copy my file it's only going to be three or four lines so version version is three at the moment and then you need a services tag and if you move over to the mailer.yaml file which was installed when you installed the mailer package just need to grab this first part of this environment variable mail at dsn paste that into my services into my docker compose file sorry change it to lowercase colon and i need to get an image so i'll go over to the docker hub and so what we're looking for is this one here shickling forward slash mail capture so i'll paste that in and then ports just copy what i put here and so we're almost done with that what we need to do then is add our routing so i'll just remove all this stuff here and i'll just add a route to an email endpoint we need to spin up docker compose and we need to spin up a server so docker compose up dash d means run this in the background giving me my terminal back and then it's symphony server colon start again dash d runs it in the background and just one last command if you copy this now open call on local colon webmail what this will do is open our mail capture in our browser window so there we have that this is where we'll get our viewer emails once we send them just gonna grab the address for the server just need to paste that to browser window with the email uri tagged on the end email sent okay so here we see our email and there you go thank you your order has been placed so we'll check out the other bits i'll make it a bit bigger so you can see make sure everything's there so from address is correct the two address is correct so that went okay but it wasn't the most visually appealing email if you want to send something looking a little more professional we're going to use a templated email next in order to use that we need to install twig and a couple of little twig extras which i'll explain as we go along so if you just copy this which you see on the screen now and we'll let that install okay we're good there so instead of sending an email we're now going to send a templated email the process is very similar instead of creating an email create a new templated email and a lot of the methods are exactly the same for example from so instead of hardcoding a string in here i'm going to create a binding which will be available throughout the whole app so if you come to services.yaml and under this defaults tag here create a bind tag and i can call this anything i'm going to call it app email i'm just going to set it to a string so i'll call it same as what we did before sales app dot com and so now you can inject that into any of your constructors and you can also inject it into your controller method you can't inject it into methods of other classes just your controller so that's looks good let's add a two i'll just start coding our example email again we're going to add a subject your order has been placed and so this is where things are a little different from before before we just said html and we added a a paragraph with paragraph tags here we're going to point to a tweak file if you're unaware twig is the templating engine which symphony uses to produce front-end files what you can also do is this you can add attachments you could have done this with the email as well it's not just a templated email but i thought i'd show you this now so what i'm going to do is i'm just going to drag in an example invoice and that will go in public i've created a folder called pdf inside public and there i've added this example pdf what i'm doing here back in the services jaml file is adding a couple more bindings which will make it easy to get to that path where i place the pdf so at the heart of your applications it's a kernel and that has various properties one of those being the project directory and so i'll just tag on public onto that and create a public directory variable and then i can just inject like i did with the app email and so public directory pdf folder and then just the example invoice pdf file this last method will be context this is pretty good because this enables you to pass random variables into your twig template so what we'll do is we'll take a delivery date and so i'll just create a date object and add three days onto the current date and we'll also just create a random order number between five and fifty thousand should do us and that's really it so what we need to do now is create this uh twig template so we go to templates and then i'm creating directory inside of templates called emails and then inside of that i create order confirmation dot html dot twig what i've decided to do here with the two method is pass an address object instead so you can pass a string or you can pass an address object and what i want to do is have an email address and also the um addresses um first name so that i can make the email a bit more personalized now you can say thank you whoever the email has been sent to and the way to print variables in a tweak template is by using curly braces so two opening curly braces two closed curly braces and then the way to print that name is email.2 name your order has been placed and like i said when we create the context method you can now pass in those variables so again opening and closing curly braces your audit number is order number that will print your order number and the expected delivery date is and what we'll do here is we'll use a filter so delivery date we'll use a date filter in order to print it in a more uh human friendly fashion okay that looks good let's give it a go refresh the browser let's check our mail capture excellent so everything's there the variables have been passed in we also have the receiver name and we can click on and download the attachment so so far so good now we're going to go ahead and make things a little prettier and we'll use those extra tweak packages which we installed so normally you can't add style tags to emails because you see your email rendering applications don't allow it and you have to add your styles in line but with the twig css inliner that we installed we can actually do that so let's do that now i'll style the div we'll just give it a font family and we'll also style this heading give it the first color that pops up which is purple okay give that a refresh let's go and check out our new email there you go it's got a purple heading gun obviously it's got better looking text but if you look at the plain text version our styling has been added to that so the way we get rid of that is by also having a text template as well as the html template and we just strip out all the html tags so we'll change this to order confirmation.txt.twig and we need to create that file so we'll create that in the same folder so that's inside templates emails and then we'll call this order hyphen confirmation.txt.twig and like i say all you have to do here is strip out all of the html tags go and give that a refresh and then check out our new email and if we click on plain text there you go all the styling stuff which was there before it's now been removed that concludes sending emails in symphony maybe at some point in the future i'll follow this up by showing how to configure third-party transports what i definitely will do is i'm going to follow this one up with a short recording on testing sending emails so look out for that one in the meantime if you're not already subscribed to my channel put you on youtube to show you more of my stuff all you need to do subscribe and click the little notification icon i release new material roughly every 10 days usually less actually and if you want details of my schedule just have to click on the discussion tab on my home page and all the details will be there
Info
Channel: Gary Clarke
Views: 12,702
Rating: undefined out of 5
Keywords: Gary Clarke, sending email in symfony, symfony 5 send email, learn symfony framework, symfony mailer, mail catcher, schickling mailcatcher, symfony templated email, twig css inliner, view emails in browser, symfony 5 tutorial, phpunit testing symfony, php testing tutorial, php unit testing symfony, how to test sending email, symfony 5 testing, symfony workshop, garyclarketech, test driven development, php how to send email, php how to send email with attachment
Id: -ifUF4DJlME
Channel Id: undefined
Length: 14min 28sec (868 seconds)
Published: Wed Oct 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.