Laravel Mail Basics 2023 | Send emails with Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey i'm matt today we're talking about everything you need to know when getting started with laravel mail we're going to be going over creating new emails customizing them and how to view them while working locally plus a couple other things so yeah let's go ahead and get started okay to get started i just have a basic brand new larval app right here uh all i did is install the dependencies and it's basically completely brand new and then obviously i'm running with the pre-hp artisan server okay so first thing we're going to do is go ahead and just make a controller with php artisan make controller and i'm just going to call it mail controller all right once you make the controller we're just going to do a couple basic setup things so go ahead and open web.php we're going to import our our controller up here and then we're going to change this route to just return a function in our controller we're going to go ahead and create that method in our controller now okay so now our one route goes to our mail controller and hits a method called send mail and our send mail method does the exact same thing that it the route used to do so it just returns the view right now okay so when it comes to mail and laravel it's actually pretty easy to do uh so we're just gonna go ahead and create our first email and you do that with an artisan command and the artisan command is php artisan mail make and i'm just going to call it sign up so this is the name of the of the email mail created there we go all the files are going to live in app mail and then sign up this is the one we just created okay so now let's just take a closer look at our new signup email so this build method is actually what kind of like generates the email and all right now is it's doing is it's pointing to a view so uh we don't actually have this view right now so we're gonna go ahead and create that so i'm just going to go inside resources views new file and then let's just call it signupview.blade.php and now inside the new email view i'm just going to put some basic text here that just sign up here okay so we created our email view and uh we can actually remove this first part here where it's a view dot we actually don't need that anymore because this view method right here actually points to our views um folder right here so it's just gonna look for this file inside this uh this folder okay now back in our mail controller uh we can go ahead and paste this uh line right here so all it is is just mail two so this is who you're sending it to and then this is the email you're sending so we're gonna have to import these two at the top so let's go ahead and do that so to import mail it's like this and then import your own email like this so right now all we're doing is we're sending an email to fake email and we're sending this email template now if you actually go ahead and try this like if you refresh the page you're going to realize that nothing actually works like it's not you're going to get this error and that's because we haven't configured our email to actually be like sent yet through a provider so let's go ahead and do that okay so for our email provider we're going to be using mailtrap it's a very well supported by larval and it's probably one of the easiest tools to use so you're going to go ahead and create an email or you're going to go ahead and create an account okay once you sign in you should have this demo inbox right here if not just go ahead and click add inbox but i'm going to use the demo inbox and then this is basically where all our emails are going to show up so let's go ahead and look at our integrations we're going to set it up with our larval app so just scroll through here and we're just going to look at laravel 7 here and this is pretty much all we have to do so inside our env file we basically just have to like copy these parameters these ids and then just paste it so we're going to go ahead and do that okay so inside my env file i'm going to pretty much remove all the existing mail ones i'm gonna paste in the new uh mail trap ones okay so once you set up your env to have your mail trap credentials you can actually go back to your laravel app and once you refresh the page you're gonna see that our email actually now shows up in mailtrap there it is right here sign up here pretty basic but there you go you got emails working okay so the next thing we're gonna do is basically pass parameters into our email because basically say if you wanted to write something custom here like a name like say sign up here bob or whatever like you need to pass in that value into the email view so let's go ahead and do that in our mail controller we're basically just going to pass in a name like this okay so i'm dif i'm going to find this name variable i'm going to pass it into signup so now back in our sign up template uh we're gonna have to define a variable here once you set up a variable like this in your mail file you can go ahead and use it in the view so let's go ahead and copy this go up here into the view paste a name save the file and now when we refresh our app the second email should have a name and it didn't work what happened okay so i forgot to actually add the variable to the constructor so in your constructor in the mail file you're going to go ahead and pass a name and then you're going to assign it to the class variable like this so this dot name or this name equals name and now when we refresh our app it should work yeah there we go okay so our email here now has sign up here bob and yeah so that's basically how you pass in variables and you can kind of pass in anything you want here now there's actually a lot you can do in this build method and there's a lot of like helpers that you can use uh for example like if you want to set the from variable i mean the from email you can go ahead and do that by just passing in a method called from and then in it as a parameter pass into actual email same with attaching files basically the exact same thing you can attach files with just like an attach and then a path to the file if you want cc some people to the email you can add cc like this so yeah as you can see there's a lot of stuff you can actually do in this build method if you want to change the email attach files or cc users or other emails i mean uh but yeah we're just gonna kind of keep it simple for now and um go over some more generic stuff okay so looking at this email you can kind of see it kind of looks kind of boring it's just like plain text and you know there's not really much to it uh laravel actually has like a really good template that it comes with for like an email template and it actually looks pretty good so let's go ahead and set that up okay so if you want to use larval's mail template it's actually really easy at the top of your blade file in the view you're gonna just import this component like mail message like this and then at the bottom of your file you're just gonna go ahead and end the component like this and then they also have other components like buttons so let's go ahead and just add a button like this and we're gonna need to end this component as well and then inside the in between this like component tags you're going to go ahead and just like add whatever text you want so just call it like button or like i'll click here and then the url you can set this to obviously whatever you want i'm just going to set it to like google all right so yeah and it's pretty much that simple so um this is basically all you need to do to use the laravel template but one thing you need to keep in mind is that uh you need to switch this from view inside your mail file and you switch it from view to markdown this way it will like trigger larval's template so let's go ahead and check that out so refresh the page and there we go so this is uh yeah this is basically larval's new like default template um so i think it looks a lot better obviously than you know like a plain text email i realized that to get the button working you actually have to add like https at the start so i just change the link and then obviously when you click the button it brings you to google right nothing crazy there okay so the next thing is you might be wondering like yeah this looks great and all but like you know has larval's logo what if i kind of want to like customize a couple things it says larval at the bottom like i don't really want it to say that you know stuff like that so you actually are able to customize this so let's go ahead and do that okay if you want to customize the template uh the first thing we need to do is run this artisan command so it's vendor publish tag laravel mail just like this and this is basically just going to kind of give us access to the laravel files that are like that generate this template so let's go ahead and run that okay and now you can see inside resources views we have this new vendor folder and then the mail and basically this is all the component stuff let's go ahead and just like figure out where this logo is and change it okay so it turns out that the larva logo is inside the html folder and then the the file is called header.blade.php so this is where the logo is uh we can go ahead and just kind of change it to whatever we want i'm just going to change it to this random picture of a dog i found online so now when the email is sent you can see that the picture is updated and uh yeah it's a dog and now for the last part this larval like the all rights reserved larval part if you want to change that it's actually in the message.blade.php file and then it actually just works off of whatever your config app.name is so you can actually just change this in the config or your env file and i'll like i'm just going to kind of hard code this to like mail app like this and then yeah so that should be it so let's go ahead and test that and yeah there you go you can see the larval text got changed to mail app and that's basically it so um i think this is kind of like a quick good overview on the basics of laravel mail and you can get started real quickly with this obviously there's like a lot more you can do with rfmail you can kind of delay messages you can queue them up if you want um so but those are a little bit more advanced things so yeah you know if you want me to make videos on that feel free to let me know yeah thanks for watching guys see ya
Info
Channel: Matt Socha
Views: 34,530
Rating: undefined out of 5
Keywords: laravel, laravel mail, mail, php, laravel mail php, laravel 8, mailtrap, laravel mailtrap, laravel mail views, mail views, send emails with laravel, laravel email, email mailtrap
Id: xigpoxOW1MY
Channel Id: undefined
Length: 9min 30sec (570 seconds)
Published: Thu Jan 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.