Send Emails with Laravel Mailable Class (2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys is caught Rossum here and today we are going to talk about how to send emails with laravel for this tutorial I'm going to use levels mailable class which is really easy to use so let's get started open up your laravel project and open up the console then type PHP artisan make mail and the name of the mailable class you are going to make I'm gonna name it sign up email hit enter this will create a new class inside our app folder and inside that mail directory so this is our class so as you can see there are two methods as usual the constructor and there is a special method called build so this is where you can define all your email configurations like the subject the from name and address stuff like that this build method returns a view which does not exist yet but we are going to create it in a minute and that's where we put all the text and all the HTML content in our mail so that's where the email template resides so let's create that view first I'm going to open up resources we use and inside that folder I'm going to create a new directory called mail so that I can use it separately for email templates I'm going to create a new blade file signup email plane dot PHP so this is the email template of my sign up email here I'm going to put some dummy data it doesn't really matter you can put anything you want you can put CSS maybe even animations you can put anything you want that's about HTML I'm just going to put a basic text so this is my email I'm gonna hit save and our email template is ready and I'm going to change this to the template we just created this is a mail dot signup Beeman sign up email right now we are returning the correct view so this will work as the email template great then I'm going to add some configuration in the build method first I'm going to put the from email the email is from this will be my my website's personal my personal email or my website's official email and I'm going to put the subject you can always check you can always check the official documentation to see all the configurations you can have in this method I'm just going to put the basic contents and basic configurations for the subject I am going to put a string like this welcome new user so like this so I have already put subject and from email address and I also I can put the name for the from the from name which is I'm going to put my website name called awesome like this so it is ready right now as you know the email content can be dynamic the email should be personalized for each user for example the name of the receiver the the other stuff profile picture so and all so this content should be dynamic so that the content of the email will make sense in order to make it dynamic we need to pass some data to our email template so we can do that with mailable class what I am going to do is I am going to put a parameter in the constructor like data you can name it whatever you want and I put it like this let's say I sign up male data and I am assigning that to this so that I can pass this data to the view that works as the email template it's like male data and I'm going to put it here like this so I can pass dynamic data to my email template right we are almost done with this mailable class now we need to call this method from wherever we want for example when the user creates an account or when he sign up for a newsletter occasions like that this project doesn't have any kind of functionality yet so what I am going to do is create a custom route and using that route I'm going to call that method so I can test whether this working or not all right I am going to create a new controller PHP artisan make controller and I am got a name I'm going to name that male controller he take that so our control is created so inside that I'm going to create a new method public function same email inside this method I can call the mailable class so I can test whether the males are sending or not first I am going to define some dynamic data I want to pass to my email template for example I'm going to pass the name of the user so I can put that name in the email template so I can personalize my email for each user this would be like I'm going to put a name that Hindu which is my name by the way and I'm going to put some other data like verification code something like this so you can pass whatever you want and then I need to call mailable class I'm going to call male and then the to method which defines the email address the recipient's email address I'm going to fill that later then the send method so for the send method I'm going to pass the mailable class we created earlier as the argument so we'll be like new sign up email like this so when you are typing for this IDE the required facades and all classes are imported here if you ID'd the IDE doesn't support that make sure you import them manually all right now as a recipient email address I'm going to put one of my working email make sure you enter your email correctly there and then I'm going to pass this dynamic data as the argument of this mailable class right this is almost complete now what we need to do is call this method manually this is just a basic project and it doesn't have any functionality yet so I'm going to call this method using a route I'm going to open up my route folder and open that web dot PHP so in that file I am going to define a new route it will be a get route and the URL is like test email and the control ice male controller and the method we just created is send email right and we can define a name as well it will be something like test email right next what we want to do is add the configurations and credentials of the sending email address to my dot env file by default these details these configurations are added like this but you need to fill up these credentials and configurations with the email address you are using as the sender's email address which is the official email or your personal email you are going to send emails with right I'm going to put my email details like this right and now this is ready to test on the browser I'm going to open up the browser and I'm going to just paste the URL of my project and the route we just created test email we don't have get we don't have any response which means it should be working let's open up my mail and let's see whether we have received an email or not and we have is it a new email from our website with the text we put in the in our template great and one thing I forgot to mention was in order to use Gmail SMTP server with one of your Gmail account credentials you need to enable this less secure access option in your Gmail account in your Google account so you can access that you are using using my account google.com slash less secure apps so make sure you enable that so that you can send email using your Gmail credentials all right now we want to do is we can send emails now now just a little tweak to the end of this tutorial I can of course use one of these personalized data in the email template what I'm going to do is I'm going to put up we put that variable V pass in this mailable class it is mail data I'm going to use that variable in this template like this here the data we pass is name which is comes from here and we define that here right so we can test it again I'm going to read out that trout or we get an error let's see what we have messed up it says trying to get the property of an object so of course we are calling this like an object actually it is not an object it is an array so let's put the property like this and reload the page now we get a blank page which means it should be working let's see we have a new yes we have a new email I'm going to open up and yes we have included my name in the email so the template in the template we have included personalized data so guys it's working so we have created a mailable class and we did a little customization and put some personalized data in the email and finally it is working and we successfully sent an email without an error alright so let me know what you think in the comment section and of course hit the subscribe button for more videos like this in the future so thanks for watching catch you guys in the next one [Music]
Info
Channel: coder aweso.me
Views: 14,996
Rating: undefined out of 5
Keywords: Laravel, Mailable, Laravel Mailable, Send emails with Laravel, coderaweso.me, send emails with google smtp server, send signup email laravel, simple mail with Laravel, send emails from localhost laravel, send emails with dynamic data laravel
Id: yEPK2LpbgDk
Channel Id: undefined
Length: 16min 49sec (1009 seconds)
Published: Wed Apr 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.