Nestjs Send Email using the Mailer Module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this video we are going to learn how to send emails in nestjs application using the popular package node mailer but to do it nestjs way sort of we are going to make use of this nestjs modules SL mailer which from what I've seen is just like a wrapper on Top This node mailer package but this gives us some njs module that we can configure as we like and and start sending off the emails to get started let me first create a nest application so I'll go somewhere my computer using the C I say Nest new let me give it a name my nest whatever I will select npm for this and wait for the installation to complete now I can go into the project my Nest all right so let's go ahead and install this module this is going to install node mailer the underlying package and the next modu mailer so copy this over to the terminer and hit install while that is going on the next I want to do is the credentials needed to connect to to send emails I don't want to hard code the username and password no credentials needed to send the email in the code so I'm going to make use of environment variable for that so let's go ahead and install the next JS module that helps us to work with environment variables I'm going to copy and install nestjs /c config I will paste that over here the packages are installed let me open the project in my code editor I will go to app. module. TS here I want to import and configure the config module and the node mailer module so in the import array I'm going to say config module. for root no much configuration here with import like this it is going to expect that we have a file called EMV at the root so I'll go ahead and create a file called EMV this is where we're going to put the credentials for for sending the emails in the import array still let me go ahead and import the mailer module so I'm going to say mailer module do for root or for root as sync and then for the configurations I'm going to say use fact Factory I think here I want to inject config service so we can read the information from the EMV file config service which is imported from the config module and then I want to access that over here config config service of type config service me close this so what do we configure in the node mailer for our snpt these are some of the configurations we can do let me use a simple one so we are going to specify the host for the email service the the port uh whether to use SSL and uh the username and password so to do that we pass the key transport and we have post here this is going to come from config service doget it's going to be of type string we are going to add this in the EMV in a moment let's call it mail host then I'm going to copy have a typle here going to copy and paste this for the other fields then we are going to have Port mail port and and then the next thing is um let me just copy [Music] this paste it over here and then replace the hardcoded values yeah and here so for this one we say m user and this one ma passw for this we leave it at first I'm on Local Host so uh I think I'll have issue with SSL if enabled so this is like the basic requirement that we need to have but I want to add one more thing for every email we need to have the sender email we can configure a default here that if we don't specify in the at the point we are sending email it's going to use that default so we can say from that is the sender of the email um this is also going to use a value from from. EMV so let me just call this mail sender now I can go to EMV and fill in this values so over to. EMV here we going to have M host by the way this values that we need here what you supply in EMV depends it's your own value depends on what uh email service you are using if you using Google if you using Zoho whatever you are using to send email you get those credentials there and come over here and view them let me quickly add the other on for the purpose of this tutorial I'm going to make use of this service called M trap it provides you with the environment and credentials you can use to send the email get them in this inbox here so you can use whatever you want you can use this ma Trap by signing up then once you click on this show credential it's going to show your own credentials what you can see here are my own credentials which I'm going to reset afterwards so from here you can copy your uh credentials over to the emv5 this is the host uh for the port I'm using 465 and then this is my username this is my password all right so I've filled in my own values this one is up to you that is the sender in this example I'm using support at example.com now let's go ahead and add a method for sending sending out the emails for this instead of creating a new modu or service class I will make use of this app. service.ts file so I'll add a function called send email this is going to be SN but we need to bring in the the mailer module the mail service from mailer module so in the Constructor I'm going to import uh private readon mail mailer service I think that's what it's called mailer service and next I want to bring in is the config service because I want to get a value from there as well private read only config service conf config service all right then to send out the email we can say um let me just make this function a little reusable so we are going to pass the variables from sending of the emails for this let me just create a type here type can even export it export type uh send email D you and then I'm going to say that this parameter here is of type send email D then to send out the email we can say const let me say result of that action to will be equal to await this mailer service do send email this is going to take some required options for example um from that is the sender but this is optional since we have already configured a default here in the app. mod. TS import array but let's just have it so it means that in this D we are going to have from the is going to be optional this can be a string and then uh let's grab that from const equals DTU so this should have a property from or let's call it sender so it is not confusing to the person calling this method so this is going to be sender yeah then the okay this is going to be sender the next thing we are going to do is to have to this is the recipient the person that is going to get the email so this is also going to be of type string or address or let me see address I think we can import this type here so we're going to have two which is going to be a string or address U Can I import that I think so and then we also grab it from the dto to or let's just call it recipient once again not to confuse the the color recipient it can actually take multiple recipients like array of those recipients so let's call it recipients and then here we say recipients these are the people that will get the email the next thing we can have is the subject of the email this is going to be a string so let's have subject of type string and by the way this sender can also be of type address so either a string or address but not multiple sender you can have multiple recipients right let me just make it address and array so it show you can have only one if you want to send only to one recipient at a time but that is going to be array of recipients then for subject we say that is a string and we can get it from here as well subject here we can since it is the same name as here we can just leave subject like that and then we have text this is going to be the actual message the plain text message that we send to the recipient once again because we want to pass this over let's say text is going to be of type string and we can grab it from here as well text and this guy is text this is a plain text if you want to also send um if you want to send HTML so we can have HTML here this is going to be of type HTML which is also a string so here you can do normal HTML uh markup that is valid for email and here is a plain text version of that HTML so so here we can so have the HTML another thing you can do about HTML is to use template so this module this ma module has options for you to configure um templates using something like EGS or um POG handle bar but it's a matter of choice you can have HTML as a string passed to this which is the is the option I think might be more more flexible for example the HTML can be from anywhere can be stored in database you just pass it over here but if you prefer to configure and create some um EGS or whatever template that is up to you this is how you can configure it you can check this documentation all right so um what do I do with this result it is not reading in there but let me just return the result because because we are trying to call an external service something might go wrong anyway let me just put this in a TR cach block I'm going to say try cash and leave it like that I don't have login implemented so I'm just going to put it in the console say that error has happened and the error all right to give this a try um okay I wanted to use this config service to let me just use for the sender let me change it a little bit so I'm going to remove sender from here I'm going to say that const sender is of type address and it's going to be equal to recipient no dto come on recipient are you kidding me the sender if the sender is supplied we use that one otherwise we're going to have the name which is where I wanted to use the config service to get um a string which you can now add to your you can add to your EMV file let me call it app name make sure to add that in your EMV and then this is going to take another thing address this is the actual email address that sends the the email and let's call this mail sender which is also from the EMV by the way this is just going to override the default we already configured anyway you get a point so this is optional uh the entire thing about sender is optional since we already have a default but let me have it here for demonstration purposes and then then I'm getting this error because sender can be a string a string or address all right so let's go ahead and give this a try I will go to don't like this warnings in this guys I go to the controller and then have an endpoint that when we call it is going to send off the email so this is going to be a post request to slash send email whatever and here we are going to get snc doesn't matter does it we're going to have send email we are not going to pass some data from request Etc just to keep it simple I'm going to hard code some values here so this information is going to be of type let me call it it you it's going to be of type send email dto and what is this expecting is going to expect the recipient the subject etc etc I we also like to have the sender on though it is optional and the email is going to come from name let's say from human resources and address is going to be HR at example we are going to see we are going to test when we don't have this sender whe it's going to use the the the fullback then for the recipient it can be one it can be multiple we already know that so here I'm going to say John do and John no at example.com you can add more here then for the text and HTML I'm going to save us some time going to paste these guys here so this is a plain text uh version of the HTML am I missing something here I don't think so all right now I'm going to return our wait there sing so this do app service do send email and pass the Dil all right so we can go ahead and start the application and give this a try we will come back and change things up a little bit so I will go to the terminal and run NP and Run start Dev to start the application uh what is going on next res the dependence of maer config service config service we imported config service to this is app module so okay so I think the error is coming from from the config service used here because we need to we can import the config service here of first we can make it Global so we don't need to import it say it's Global to true and I guess that should resolve the issue but um don't like GL so another thing we can do is to import specifically for where it is needed so here we are going to import config module and that should also resolve the the issue I will go to postman now and send the request to Local Host 3000 send Ma and post request which is the end point we just defined and if I send a request we wait it is trying to send the email I guess and we get this response showing that the email was sent out now let me go to mail trap and check the inbox you can see that we just got the email few seconds ago I don't know why it says no subject we are going to check why but the email you can see is from HH example.com and the recipient is joho joho example.com this is the text we sent you can see this is HTML this text here is green and if we view the source this is the HTML we passed there this is the plain text version of the email all right so let's see what is the issue with the subject I'll go over to the controller oh my we did not send the subject so let's give this money money secret whatever all right we have seen that the email came from hher let's remove this and test the case of not passing that email of course that should also work sending the email and taking some time is sent we check now so now we have the subject and you can see that the email came from support at example.com which is the value we configured in the in the dot actually we repeated that configuration here what I'm going to do is to remove this completely and we try to send again because we are doing the same thing setting the same default we set in the app. mod. TS over here so let me send it one more time and we wait the email is sent again and once again should be from support refresh this guy so once again it comes from support which is the very default we configured okay like I said before um you can pass whatever value have have here whether it's coming from some API some from the request from database whatsoever it is that flexible let me just hard code a value here let me say count announcement to be equal to use back tick here and past that guy there so over here what I'm going to send is the announcement right it is that flexible and the request and check the email we get the thing I just ped all right guys so this um has covered some of the things about sending email with this package not mailer but you can check the documentation to go more in depth um and configure things according to your need for example there this thing called pulling I I think when you are sending many emails and other things you can check out here I hope this tutorial makes sense to you until next time enjoy [Music] coding
Info
Channel: ZestMade
Views: 1,684
Rating: undefined out of 5
Keywords:
Id: DAAxWEPCARo
Channel Id: undefined
Length: 25min 24sec (1524 seconds)
Published: Thu Dec 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.