Laravel 6 Beginner - e14 - Sending Emails

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] let's bring in an entirely new concept into our application let's talk about sending emails it's a very important part of most applications that we notify users of different events that have happened or just simply sending them a message every modern application will need to send an email at some point Lenovo has incredible emailing capabilities and it makes it very very simple in this episode let's explore through that the first concept I want to bring you to is the mailable concept a mailable concept is the entire package that letter veau has put together for sending an email the way that it does it is using the views the same way that we are used to something like customer that index view we can now generate a view which is an HTML email that gets sent out to the customer there are several different parts to how to actually make this work because most of the time you're going to be using a service to send the actual email we'll get to that at the end but first let's create one of these mailable if we jump into the terminal we could say PHP artisan help make mail so this creates an email class every mailable will have a class as well as of course the template that you're going to be sending to a particular email address now in levo you can send just a raw email but it's not very common to do that so I simply want to show you the markdown version of that so notice that we have this - M flag whenever we pass that it actually creates a mailable which is really what we want but let's start hacking through this and I think it'll make a lot of sense as we run through it the command will be PHP artisan make mail then we need to give this mailable a name let's say welcome mail and then let's pass that - M flag now the - M flag does require some parameters as you see here and what they are is the address to your view very similar to how we did customer index let's create a new directory for email and then just have the name of our mailable so we could say emails dots welcome so this will generate an email directory and then it will have a welcome that blade that PHP it's going to be a view it's gonna look exactly identical to an HTML view all right let's create this and now let's explore what it just did for us we now have a new mail directory right in here inside the app and if we open that up we'll have that class that I was talking about welcome mail and it extends malleable okay that's pretty cool what about this view that I keep talking about under resources let's go into views we close customers notice that we now have there's new emails directory right here and inside of there we do have a welcome blade so the first thing I want to show you is how could you render this in the browser it would be nice to be able to see this email and letter well does make that very simple let's jump into the web that PHP file and anywhere in here you know what we'll even reuse this right here we'll say slash email instead of returning a view return a new welcome email and notice that I am importing that up here at the top but you have to put this over the top so that it knows which class this is so returning a mailable class actually gives you the mailable in the browser what we would have to do is visit slash email let's give that a shot go back into Chrome in my project I'll go ahead and visit slash email and there is our mailable okay that's pretty cool so it already even looks pretty good where is this coming from well that's that mailable that I was talking about this one right here to notice introduction from coders tape there we go there's our mailable we just say here welcome to our cool application and we can get rid of this button as we don't need it then we hit refresh and this could be a very simple welcome email you may be wondering where is this Lenovo name coming from and how do I change it what you have to do is change your environment file let me close everything up and if you notice here you have this dot EMV file we've kind of already touched it whenever we set up MySQL right over the top here you have app name it says level let's change this to something like cool application you're going to have spaces you are gonna have to wrap this in quotations and now you will need to restart PHP artisan surf to be able to actually see that change and I'll stop PHP artisan surf and let's go ahead and run that again PHP artisan surf if you don't do that then it won't reflect the change and now we do see the change again so that's just it if you don't see the change it means that you did not restart your PHP artisan surf ok well this is pretty cool but how do I actually send it for most of development you can use something called mail trap and just as a very quick side note here if you scroll into the mail section of your DMV file notice that mail trap is already set up for larvae all you need to add is your username and your password for that you could create a free account in mail trap why would you use this well this is a testing inbox this will let you send emails without actually having to send emails it simply keeps them inside a sandbox so you can do your testing I've already signed up signing up is free so I'll just simply log in and grab my credentials and once you are logged in this is what you're going to see and here is the username and the password now don't copy my username and password you need to have your own otherwise you will never see your messages I'll go ahead and grab my username and I'll paste that into my DMV file and then I'll grab my password and I'll paste that right here then the most important step here is you need to stop your PHP artisan serve and restart that one more time let me go ahead and stop that with ctrl C and then let me start it up again okay so now my layer of application knows about it so how do we send one of these maila bowls we can simply do it in line right here for now in my web you can say mail and the one that you're looking for is illuminate support facades mail make sure that you add this line over the top so that it knows which one is the correct one okay mail I want to send a new mail to would you say email at email com and want to send them a new welcome mail that's it that's all it takes now we'll go ahead and refresh this page here should notice much of a difference but now when I go into mail trap notice that I do have my welcome mail and there it is so this is a nice way that you can test your emails and do everything that you need to do without actually having to send any emails as one last step what do we move all of this logic into my controller so that way when I create a new customer it sends them an email automatically how cool would that be I'll go ahead and actually comment this out completely as we don't need it let me go into my customer controller and in the create section after I've created my customer let's send them that welcome email so we can say mail and again that's illuminate support façades mail make sure that you import and add this line over the top otherwise it's not gonna work let's send it to and we're not gonna have a hard coded address we have the customers email at this point so we could say send it to the customer email I want you to send them a new welcome mail and welcome mail will also need to be imported at the top right here make sure you add that line again so that it works correctly so that's it whoops I put this in the wrong method huh let me actually cut it out and it needs to go right here in store there we go that's where it needs to go I'll paste that in I'll say my name is Victor and this would be Victor at cool calm I'll add new customer my customer gets added I go to mail trap and there we go we've got a new welcome email for Victor at cool.com and just like that our little application is sending out emails when it comes time to deploying this you will need to use some sort of service I would probably recommend mailgun that's mailgun comm as they are probably the easiest to set up this is what will actually give you the ability to send email in your application now mailgun is free most of the time up to 50,000 emails a month all you have to do is create an email and have a domain name so you can send out your emails but that's a bit of a more advanced topic but just as a quick side note this is probably what I would recommend for you to use whenever you're actually trying to send some real emails but for now mail trap will do great I use mail trap every single time I'm in development and then again in production you will have to use some sort of service
Info
Channel: Coder's Tape
Views: 65,691
Rating: undefined out of 5
Keywords: laravel, laravel 6, php laravel, laravel service container, laravel request lifecycle, laravel request, laravel project, laravel 6 tutorial, laravel 6 new features, request laravel, request life cycle in laravel, laravel response, laravel 6 for beginners, laravel 6 crud, laravel send email, laravel send mail, laravel mail, laravel mailgun, laravel mail send, laravel mailables, laravel mailtrap, laravel mailtrap send email, mailtrap laravel, mailgun laravel
Id: Ny-mloJuoAI
Channel Id: undefined
Length: 9min 44sec (584 seconds)
Published: Sun Nov 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.