Emails & Mail Trap testing | Laravel 10 Tutorial #29

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back on today's episode we're going to take a look at sending emails with a lot about so let's get started guys now what I would like to do is send an email whenever we register any user right now we don't send any emails so I want to send them a welcome email okay thank them for joining the website so let's go ahead and learn how we can do that so the first step before actually doing any email sending is you need to create a mailable class object okay so and this is kind of easy to do you can use phpr to send so go ahead in your terminal and type in PHP artisan make mail and then enter the email name you want to have so I'm going to say welcome email you can name it whatever you like maybe like new user registered email whatever you wanna whatever kind of email you want to send just have the name and usually I like to basically add email at the end so it's easy to search for these I'm gonna hit enter and after you run it laudable will go ahead and create a new folder inside your app directory called mail and then have your basically email class inside it and basically emails in laurable is going to be a simple class that extends a mailable class here as you can see now inside it you will by default have three methods okay so you're gonna have an envelope method which is going to be basically like the header of your email this object the from email maybe if you have any CCS things like that camera copy you can Define them here this next one is going to be a method for the content as the name suggests it's basically what the user sees right the HTML or the markdown of your email goes here and then you have a last method for attachments if you want to send any attachments you can Define the files here okay we will go through all of them one by one so with the envelope the easiest thing you can do is you can just update basically this object so for instance here I'm going to say welcome or I can say thanks for joining and then I'm going to add our app name and here we can use the configs so I'm going to say config app.name okay and we actually already have done this on our layouts if you guys remember or not okay so this is exactly what we did on our layout over here and I'm going to default define basically an empty default so you can Define your subjects here now you can also basically have the from so if you want to send the email from a specific email address you can Define it here you can also Define a global sender address which is what we're going to use so you have a lot of options here I'm going to just open up the class envelope class actually so we can take a look you have from you have two you have CC BCC reply to obviously subject we already defined we can have metadata so there's a bunch of different things we can use so by default you're just defining subjects so you have a lot of options here now for the content you can pass in a blade file here so I'm going to go ahead and the display fault does not exist but I'm going to create a folder called emails and then say welcome email okay so I'm going to go ahead and put my HTML here so let's go ahead and create that inside views I'm going to create a new folder called emails and then inside here I'm gonna say welcome email dot blade.php now if you have a lot of emails maybe like emails for users emails for creating posts maybe you can have subfolders inside your emails folder but in our application we might send like two emails at most so I'll keep it simple and here what I'm going to do is I'm going to say I guess thanks for joining joining and then here I actually want to have the username here so I'll show you guys how we can do that in a second right now we don't have access to the user and then here I'm just gonna say testing something like that okay you can create your own HTML template and add them here this is basically a fully functional blade file anything we can do in your regular layouts you can do inside the email as well now to be able to pass data to our blade file you can use there's a extra argument in this content class called wit and then this is similar to how you would pass data to your blade files in your controllers you can basically give it an array and pass all your basically data here so I'm going to pass in the user that we are sending this email to now right now we don't have access to this user so what we can do is we can go ahead and define basically a private property it could be protected it's up to you but I'm going to create a private user property and you can also Typecast if I'm going to say user user and then receive that in your Constructor when you're sending the email so I'm going to say user user and then this dot user equals user now in php8 you can technically remove this these two I'm going to keep it because it's a bit easier to understand for people new to PHP but if you like you can make this a bit shorter and in php8 and above just write this and it will work exactly the same but I think for people new to PHP this might be a bit confusing so I'm gonna basically that's how you can pass data to your email using the Constructor and then I can access it here so you can here I can say this dot user and pass the user to my blade file okay very easy to do so whatever data you want to receive on your emails Define them as a property and then set them in your Constructor that's the process so now here instead of username I can say a user name just like that okay so we have created our email class we have defined our HTML how can we actually send it now this is done basically very easily so in our case I want to send the email when a new user registers so I have my ALT controller open over here so I'm going to find where we are actually send creating the user which is inside the store method so here we are doing our validation here we are creating the user I'm going to store it in a variable so what I'm going to do is basically after we create the user I'm going to go ahead and send the email okay so I'm gonna say represent mail class that Laura will provides is called facades so I'm going to say mail and it's inside illuminate support massage mail okay and here we can go ahead and it has a static method called two we can Define who you want to send the email to in our case it's going to be user.email and then you can basically call send the send method and these are chainable and the path in a new instance of our email class which was welcome email yeah now this welcome email obviously our Constructor expects a user okay so I'm going to also pass in the logged in user here and that is basically all you have to do to send emails yeah just one line of code now if you want to add some additional things to your email maybe you want to have a cc for this specific email you can do that there's a CC method here you can do there's also a BCC these are all available options for now we're going to keep it simple and I'm not going to add any of those actually let's keep it two lines for now okay guys so that's all we have to do in terms of code for sending emails now we don't need to also Define and set up our mail server configurations so that is not through our EnV file so if we go ahead and you open up your dot in v file and scroll down there is a section called mail there's a mail section it's kind of similar to our database information and here we can actually go ahead and Define your SMTP server details okay there's also a mail from address this is kind of the global mail from the from address that is used for all your emails okay now if you want to define a different email for a specific email you can again also Define it inside this envelope okay so we can also Define it here manually if for example for a welcome email you want to have a specific from sender we can Define it here but if you don't Define it it will go ahead and use the default here and here I have it no reply add domain name so instead of this domain name you can have your actual domain name here and then the mail from name obviously is going to be the name sending the email so by default obviously it has some I have set up mail trap here okay so if you want to send emails for testing purposes there's a website called mailtrap that allows you to do it so you guys can go register it's a free website just basically search a mail trap on Google and go on their website and create a free account and once they do that on their home page there's a section called email testing so you can just click on start testing and it brings you here and just go and inside this integration search for laudable there is a lot of well yeah here and we'll actually go ahead and give you the doc the configuration so you don't need to Define it yourself I'm just going to copy this and this is just a test account I have created using a temporary email guys so uh you know I don't care about the passwords and stuff for it but basically you can go ahead and replace it with the default that comes with loadable I have already done that before the episode I just wanted to make sure mailtrap still works I haven't used it in a at least a year so but it is that's all you have to do just go ahead and paste it now if you are on a production server or staging and you want to use something like mail gun things like that normal documentation if you go on loadable documentation and select mail they do have a section for driver prerequisites so you do need to go check there and if you for example are doing mail gun you need to follow the steps here for it to work so you need to install Symphony HTTP client for it to work and then if you are doing like postmarks they have different like explanations for different drivers there's just too many of them for me to cover all of them but I believe the mail Gun website also has a tutorial okay so if you're using any specific email service provider just going on their website they will give you kind of the details of it so similar to kind of like mail trap that they have this configuration here I believe majority of them will probably have either an article or tutorial on how to set it up for a larval okay just search on Google your email you know service provider and then laudable and they should have a tutorial on doing it and in those tutorials they usually have the configuration if you don't know how to set it up yourself but yeah so that's basically all we have to do for mail trap I have gone ahead and set that up and this will allow us to do basically testing for Avo email sense so let's go ahead I'm going to log out register a new user I'm just gonna put some random email here some random password and then let's click on submit and again on mailtrap right now I don't have an email so I'm going to say submit and if we didn't make any mistakes we should be able to receive our email now as you can see it is taking a little bit of time to send the email you can actually queue your emails if you would like laurable does have support for that so if you guys want to want me to make a video about how to queue our emails I could do that but basically that's the basics of sending an email I'm going to go ahead open up mailtrap and as you can see it says thanks for joining ideas I'm going to zoom in so you guys can see a little bit better as you can see we got the email and that's all we had to do okay and the HTML template looks exactly as we defined it now if you would like to preview your emails before you send them maybe you have an admin panel and you want to show the preview to the admin there is one trick you could do so I'm going to go open up our dashboard controller just for testing purposes if you just want to see a preview of your email you can actually go ahead and return the instance of your mailable mail email class so I can say new a welcome email and then pass in the logged in user just because we need that and if you go ahead and just display the HTML for you just for testing purposes obviously so usually the only times I have used this is when I have an admin panel and I want to show the preview to the admin so they can see what the email looks like but if we go ahead and we reload the page it says undefined property user yeah it should be that parenthesis let's reload and are we logged in or not I believe I did not log in so I need to log in because we're not logging we are getting a null so I'm going to go ahead and log in into one of my accounts and basically now that I go to the dashboard page we are seeing the email template so if you want to do a preview of your emails there is basically you can do it this way just for testing purposes obviously so I'm going to go ahead and remove that because we already obviously tested it using mailtrap and we were able to receive our emails now one more thing I would like to cover before we end it guys so again for the envelope class just if you inspect it in like PHP storm or vs code you can see all the available options we have from to CC BCC reply to if you guys want to Define all of those the same for content if you inspect it there's basically view html text markdown and width and then for the attachments what if you want to send basically some file as a chat attachment so uh basically inside this array you can Define as many attachments as you would like I'm going to go ahead and Define one attachment so basically you can use a class called attachment and it is going to be inside illuminate mail mailable attachment so there is it's already defined here and then it has a bunch of static methods on it that you could use so these are called from data from pad from storage and then from disk so you could just use any of those if you have like a base64 image you could maybe use from data I believe I'm going to use from Storage disk and because we don't have any images I'm going to use one of the images we uploaded for our user profile pictures so the first argument is going to be the disk which we're using the public disk and then the second one is going to be the image itself and all of our images are inside the profile folder so I'm going to go open up and find one of the images so we have public profile I'm going to go ahead and copy this actually okay and paste it in over here and that's all we have to do basically to send an attachment and you can copy paste this and have as many attachments as you need now on a real application obviously you're going to be probably loading this from the database since we don't have any images I'm just going to hard code it here so a new user obviously won't have any images so that's all we have to do guys and now to do test it maybe we can create a new user so I'm just going to go ahead log out create another test user very quickly I hope I didn't mistype the password let's create this click submit I'm gonna go ahead open up my inbox and we just got a new email a few seconds ago I'm going to open it up and if you go on the previous email guys I don't know if you guys can see where my mouse is is empty on the new email there is basically an attachments here okay so we were able to successfully basically send the attachment and as you can see it's whatever this is Dot PNG okay so I'm gonna go ahead and copy the link I don't know if I can actually view the image here it's going to go ahead and download it but basically that's how it works guys so yep that's the basically the basics of sending emails in Largo guys hope you learned something new if you have any questions you can ask me in the comments and I appreciate it if you guys hit that subscribe button and like the video and I'll see you guys on the next episode have a great day bye
Info
Channel: Yelo Code
Views: 3,202
Rating: undefined out of 5
Keywords: Laravel, Laravel tutorial, Laravel tutorial beginners, Laravel livewire, laravel livewire tutorial, php, php tutorial, laravel tips, laravel tutorial, laravel project, laravel for beginners, laravel database, laravel migration, laravel crash course, laravel series, laravel php, php laravel, laravel validation, larvel profile page, laravel many to many, laravel follow, laravel social media project, laravel email, laravel 10 email, laravel 10 mailtrap
Id: bwk1EEKKcN4
Channel Id: undefined
Length: 16min 17sec (977 seconds)
Published: Wed Aug 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.