Laravel Email Notification to: Email Address, User, Array of Email & Multiple Users

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys welcome to another lesson online reveal in today's lesson we are going to see how to use notification facade to send email to different types of recipients remember in laravel there are different options for sending out email notification for example you can have a class that extends the main label class so that in your code use the method side to send the notification our focus will be on the notification class and for that we are going to see how to send notification to one user in this case a user model and then we see how to send to any email address that is not a user model right so it wasn't user model we see how we can send to multiple users as as recipient so in our tool we are going to have multiple users receiving that email that's an email email that we sent out similarly we are going to do the same with just an array of email addresses and finally we are going to see how we handle a case where there are a lot right there is large number of users or recipients so for example say you have tens of thousands of users in your database so that will be the final case all right with that in mind let's move on to help us get started and go straight to the point i have created a new laravel project this is the laravel edge project in the project i've done a little bit of setup the setup i've done is this i added the announcement model this very model you can see on the screen then here i added the field level these are the columns that are most assignable by default a laravel project comes with the user model defined so i did not add this then this is the migration for announcements table you can see those fillable columns as well in addition to the timestamp and the id so that we can have some data to work with in the database cedar i am inserting some records into the users table using the user factory that comes with a new laravel project over here this is the user factory so i am inserting 21 user when i run php artisan dbcd and here is announcement controller the idea is that when new announcement is made when new announcement is created we store it in the in the announcements table and then send out email notification to recipients say hey there's a new announcement you know and finally i have this api endpoint defined here so that you can have a way to store the announcement and take it off from there starting database and send out email notification that is why i have this post request to announcements endpoint which of course caused the store method in announcements controller so that is the setup now let's get started with notification to get started with the email notification we are going to need an email client to handle that you can make use of megon google smtp whatever option that suits your need but for this demonstration purposes we are going to make use of a free um client that is meant for testing and that is mailtrap all you have to do is go to metrap dot io and sign up for free then under this smtp settings we select laravel under integration the drop down level 7 and above just go ahead and copy every configuration here then head over to dot emv file go to dot env and let's just replace those default settings we have from main menu host port username password encryption so i will delete these default ones and match the remaining two so the ones i deleted are equivalent to the ones i brought over from meltrap for the last two this is the sender of the email we can say dev at example.com close this space here our setup is complete using metrap and i forgot to mention that i connected to database of course you need to connect to database in your own local environment otherwise we cannot store announcement in the database table all right that is the configuration for email now let's head over to the terminal to the project route and generate a notification class we can do that with the artisan command php artisan make notification uh let's call that announcement created because we want to send out this notification whenever a new announcement is made so you can call it anything you want but i think announcement created makes sense hit enter and that notification class is generated we can find that file inside notifications folder you can see announcements created this extends notification class unlike the main level cluster extends main level let me minimize this a little bit then in the constructor we can pass whatever we want to use inside this class for example whenever new announcement is created we want to get the title and the description of that announcement and send it in the email text so we can say private announcement to be of type announcement that is not typescript um in the constructor you can say announcement announcement then this announcement to be the announcement passed in the constructor so um of course we can type in this if we need to with this we can make use of the announcements in the two mail method this is where we specify the text that the recipient gets in their email inbox these are the defaults i'm going to change this up a little bit the line is like a paragraph inside the email text so i will put the email description there the announcement description description this is the action button they can click it is optional you can remove it if you don't want it but let me just leave it there and say something like a view go go to app so when they click that it will take them to the home url of our application and finally this is the sign offline sort of that says thank you for the title i will make it the greeting greeting this announcement um title we're going to see how this looks when we send that an email notification so basically going to have the title of the announcement and the description basically before we move on i want to quickly point out that we're not trying to cover everything about notification right for example we didn't talk about queue which is as simple as implementing this should queue interface and if you have your queue set up by the way laravel has default for that as well it should be good to go and down here you can see we are making use of the default laravel image template you can change this to make use of markdown or blade template whatever you prefer our focus is majorly on it just on recipients right how do we send to single recipient multiple recipient user model whatever let's move over to the controller store method here we are not going to do stuff like validation or using the request class to handle validation that's not our focus here i will go straight to create an announcement from whatever that is sent in as request so i will say announcement create for the title we say request title and for the description we just say request request description all right this we save the announcements in our table for now let me just return the announcement as justin responds response json the announcement save that so time to test remember we have the end point that expect post request to announcement i'll head over to postman i have added this request body the request payload here and we're making call to api slash announcements localhost 8000. that reminds me we need to start the server so i will say php artisan serve to start the server running at localhost 8000 i can go ahead to send this request now send and we get the response as we define in the store method so a new announcement has been created this is the id and returned okay that works of course but that is not our goal our goal is to send email notification whenever a new announcement is created to send notification the email there are different options we have here for example you can consider and what if it is just one recipient um for that we can easily make use of the user the reason we are just going to do something like let me do it actually i'll just pick any user from the table from the users table but first we need to insert some records in the users table i will open a different tab here i'll say php artisan db seed remember the database here that i talked about earlier in this video that is going to insert 21 users into the users table so we can have user to work with so our first example will be sending to user the next one will be sending to multiple users and of let's have it sending to email that is not user let's say just have some email some list array of email or list of email it must always be user that also applies here so for this first case we can get any user from the database let's say user um first we take any user date this is an example of sending to single user just one recipient what we can do here is we say user notify the reason we are able to say notify let me comment this out a little bit go to user model over here in the user model you can see that it makes use of the notifiable trait right this is why we are able to just say user notify it doesn't really only apply to user model any model can make use of it can make use of this notifiable trait that is why we are able to do something like notify the question is which notification of course we create an instance of announcement created that is notification we are sending out announcements created um announcements i should have author import working let me check something in the moment check the spelling announce oh i think i made a mistake here should be announcement rated for that i will also rename the the file so so the autoloading doesn't complain now i will come back here let me see announcement created that works now this is expecting us to pass a notification um instance so we pass that object notification according to the constructor what the constructor is expecting if you remember this should be able to send notification to user because user makes use of notifiable trait so i will go ahead and save this and let's give that give that a try over the postman um uh let's just use the same announcement it doesn't matter i will send the request again because i am unlocking environment and my this is actually sending on foreground right on production environment this should be a queue that runs on the background that is why it is taking some time to to finish that process the whole store method and sending notification already you can see that we have that email sent to the recipient that is the user we picked from the database right so that works and you can see those things we are defining in the to mail method in the announcement created um notification class so this is the greeting this is the that is the title and this is the description this is the um call to action button and that is the sign of the greeting whatever it is which is what we have here degrading the description the cta and the final line there so this works we sent to only one user assuming the recipient is not a user model like we have done here let's assume it's just an email address we can send this email to the recipient using the notification facade how we do that is we send notification make sure to import from support facade that is illuminate support facade that is the right import we need to make use of here and we say route this should be mail because we are sending out email notification the next argument is the recipient so for example someone someone at example come on example.com that is the recipient finally we say notify notify we take the same argument this is that is the announcement created instance that we passed above save this so you can see here that we are not making use of user model like we did up there let me comment this out we go back to postman and try this we send the same notification doesn't matter that is sent i'll head over to metrap you can see we get that notification so to this recipient someone at example.com if you want to add the name of the recipient let's say for example this person's name is john doe you can add it like this john doe save that send one more time send come over here you can see the name appears here john doe now let's look at different ways of sending email to multiple recipients using our notification class for that i am going to comment this out first we are just going to assume we have resc hope that is the spelling just a bunch of recipients right like we have here but this is just one so what i want to do here is paste this guy paste another one let's say let's see you see at example.com let's see whatever name doesn't matter this is just an example we can still make use of the facade the notification facade to send that notification notification route is mail once again mail and the recipient of course we passed the array of recipients and we say notify one more time new announcement created and we passed the announcement so with this we can just send to a bunch of recipients from just array right and of course this is optional that is the name appearing is optional so you can remove it if that is not your requirement i will head over to postman send this one more time sent um over here you can see we have multiple persons that is getting this email so if you want to send to multiple recipients i don't mind everyone knowing that the other person got email this is how you can do it we can actually do the same thing using the user's model so let me quickly comment this out here i will pass user so for that i will say use us to be let me get the 21 users i have in the database save that we want to send to users so we click send one more time that is sent we come here you can see that all those users they got the email of course um laravel knows how to extract the email address from the notifiable from the user mode that i use this notifiable trait the next thing we are going to look at is what if we have so many users loading all the users like this might lead to to some memory issue so for that case we are going to do something that is not notification specific let me let me comment this out and comment this guy out as well what we are going to do is we are going to say user um [Music] i will say chunk chunk that is the spelling yeah so we are going to take for example 10 users at a time you can take 50 whatever number because we have 21 users i just want to make it 10 per time so this function and inside here this is going to be the users so this is about notification specific we are just taking records 10 at a time from this from this table here we can let me just copy this i mean just copy the servers on time announcement because we are inside the closure we need a way to make use of that announcement we created update so we say use announcement that error goes away here we can leave user the way it is right so let me go ahead and send the notification again the notification is sent if you come over here you can see that this time we have three new notifications that is this this and this because we have 21 users and we are taking 10 at a time so this is the first 10 this is the other 10 and this is the last person making it 21. so this works and of course what if um we don't need to extract that but let's assume i don't know what your use case might be recipients to be equal to maybe this is also notifiable stuff what i want to do here is just to block clock email from user and we come down here use this command so this will just give us array of emails from the user from whatever users that are returned from the user step we can go ahead and send this is similar to what we did up here right just similar to that let me go ahead and send sent head over here you can see that works the same way all right um so far so good guys so please hit the like button and subscribe if you have not done so to help this channel grow and so that people that need this kind of content can get it the another thing we can do here is um notice that the only thing we are seeing is the email if you want the name to show we can add comma here we can add comma here we say not like that actually let's say then and email i will send this then explain what i just did there that is sent head over to my trap you can see we get the recipients this time with the name the name the email the name the email so what is happening here is that when you use plot like this this becomes the the key and this this becomes the value and this becomes the key right so it's not the other way around that this is the key this is the value let me show you how that looks like so you understand better what i am trying to say i will just did that stop there head over to postman send it go to preview you can see what that gives us email address the name email address the name which is similar to what we have up here that is why we are able to send the email right let's find that the email comes second here in the actual sentence that is the key and the name is the valley okay guys i believe that we have accomplished our goal for this lesson we have seen how to send email notification using the notification class to single user multiple user the user model that implements that uses the notifiable trait and all that and this brings us to the end of the lesson until next time happy coding [Music] you
Info
Channel: ZestMade
Views: 9,822
Rating: undefined out of 5
Keywords: laravel notification to email, laravel notification to all users, laravel notification to array, laravel email, laravel send email, laravel mail, laravel bulk email
Id: BPNAz52fFIA
Channel Id: undefined
Length: 25min 51sec (1551 seconds)
Published: Wed Mar 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.