Nestjs Send Email using Nodemailer ONLY - Examples + HTML Templates, SMTP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's see how to send emails in Nest ja using only the node mailer package I go ahead and create a new project using the nest CLI so I'll say Nest new I'll give it a name my Nest I'll select npm and wait for the installation to complete now I can go into the project folder CD my Nest let me go ahead and bring it the node mailer package by running npm install node mailer also I want to keep the credentials for sending this email the password the username Etc in a EMV file so let me go ahead and bring in the next JS config module this is just for the environment variable files okay I can now go into the project in my code editor I want to have the codes for sending out the emails in a different modu you can use the app module if that's what you want but let me create a new module so I'm going to say next generate uh let me actually generate a resource called mailer and wait this is going to be for rest API would you like to generate cow end points no okay so I'll go back to the code now we have this maer module there will be nothing inside the service and the controller but let's add some codes before we get started I want to import the config module for the AMV so I will go to app. module. TS in the import array the mail modu that I just generated has been imported but let me also import the config modu for root so with this import statement by default it's going to expect a EMV file at the root of the project so let me go ahead and create a EMV file EMV inside here we are going to put our email credentials over to the mailer service class let's define some methods the first one I want to have this is going to give us an object of the M transport the thing we can just call send mail on to send out the email so let me call this mail transport and can say constr actually you can get this from the documentation something of this nature but we are going to change things to our own values so it says transport is this make sure to import node mailer import node maer from node maer actually this import like this I noticed have some issues so I'm going to say import or as node mailer from node mailer now we get some issue with typescript so let's add the types for node mailer because we're using typescript I will go to the terminer and say npm install Dev dependency types node mailer and that should resolve that I'm going to format this and at the end I want to return the transport transport okay so what are these values these values are your own specific values I'm going to show you an example using mat trap let me go to mat trap. iio so you could be using um maybe Google email service or whatever email service you are using what you need to do is to go to your email service provider and get your SMPT credentials for example you can use M trap which provides you a test uh some test environments and credentials for this demonstration I'll will go ahead and use my mail trap credentials I'll click on show credentials here the host is um SMTP do something something so but we don't want to hard code those credentials here in the code that was the reason for the EMV file we are going to come back to this file in a moment so let me go to EMV and Define those values so here I'm going to have M host which is this then we are going to have the port mail Port we are going to have the username M user and M pass word we go ahead and copy my credentials over for the port I'm going to use 465 among these options so Port is 465 the user I just copied from I just copied my credentials and then the password also copied from here another thing I want to have is that for the emails I'm going to send out if no from field is specified it is going to use a defa def I will Define here so for example a default like no reply at your website.com whatever so I'm going to say default email or mail from from to be something like uh some info at example.com another thing is the name that is associated with that email this is optional but let me add app name to be the application name that should be the sender all right with this credentials I can go over to to the service class now in the Constructor I'm going to inject or let N just inject the config service for me I'm going to say private read only config service oh man config service config service imported from the config modu and I'll come over here and replace these values with the items from the EMV that is using the config service so config service to get this is going to be just type hting it string and then the field the EMV variable is mcore host I'm going to repeat this for the other fields but change the values this is male Port but this is going to be a number I guess that's why the error because I'm in the local environment development environment I'm going to change the SSL to false and the user and password change to my own values as well so this is going to be user and this is going to be password mail pass word all right that should be it for the transport the next method here is going to be the method for sending out the emails so I'm going to have this is going to be async s sync send email this is going to take some parameter let me call it dto and I will go ahead and Define the type um let me create a file here should I call it m doace dos I don't know whatever here I'm going to export a type for that dto so let me call it send email D to be equal to that this is going to have the fields like who is the S sender of the email well we are going to use the default from. EMV if this field is not supply so it's going to be optional it's going to be of type string or better still it can be of type known as address which I'm going to import from node mailer so address is going to take name and address this is just a name associated with the email and this is the email address and then we are going to have the recipient pient this is going to be these are the people that will receive the email it can be one email it can be multiple to make room for multiple I'm going to say that this is going to be address array it can be one right and then the subject of the email this is going to be a string the we're going to send email HTML email so this is going to be HTML string and um the text version of that HTML let me make it optional this is also going to be a string this is going to be HTML this is going to be a text version of the same HTML email but is optional and finally for um a situation whereby in the HTML we want to replace some values for example something that can say hi John your lucky number is 37 and for someone else is going to be a different name and different lucky number we can't have a hardcoded value solving that kind of problem so for this we are going to have some placeholder in the HTML that we can substitute programmatically so let me call this place holder sub or replacement placement this is going to be um object key string and the value string all right so I can go over to the email the mailer service and say that this DT is going to be of type send email D next let me grab the values from the dto this equals dto inside here we have the from and we have the recipient the subject the HTML and we have the replacement for the placeholders now I can I can go ahead and and make use of this transport I'm going to say con transport to be equal to this dot m transport with that transport the object we can try to send an email so const result to be equal to await not this transport do send mail the send mail is going to take an object of type may. options so let me come up here and have the options const options to be of type mail from no mailer do options for the options we are going to have from if the from field is not supplied remember that this is optional we going to use the default from the EMV so I'm going to say that this is going to be from the supplied field if not supplied we are going to let me copy this thing I'm going to modify it if not supplied we are going to say that the name that is the person sending it the the name associated with the email and then address we use the values from the EMV F in this case um I have this for the address and then I have this for the name this is for the name let me fix this prettier issue next we have two this is going to be the recipient we have the subject and HTML then I can go ahead and pass this option to the call of send me here let me just return this result to remove that warning there and for error I will just put in the console console.log error in case there's any error all right we are going to come back to this placeholder thing in a moment for now let me try to send an email so I'll go to the mailer controller let me have a method here called send mail this this is going to just return await this do service do send email the send email is going to expect an argument so let's have that dto here of type send email dto this can optionally take from as already explained it's going to be an object with name associated with the email so let's say um Luc see I don't know whatever and then the address is going to be Lucy at example.com the next thing is the recipient this is going to be array of one or multiple recipients so for this let me let me just type it we say that the name is John do and address is John at example let me have another one one is fine and then we have the subject of the email locky Winer and then we have the HTML let me have a paragraph here hi John your lucky number 221 you $1 million okay and close the paragraph open the last one and say shares let me make this board say strong and strong finally I can pass the dto here okay let's start the server and try to send an email we have little dependency issue what I need to do because this mailer module is making use of the config module so I'm going to import it here config modu and the error should go away now I head over to postman and call the endpoint to send email this is Local Host 3000 SL mailer let me go back to the controller and change something so this is going to be a post request to mailer SL send email okay so back to post man I will send the request it is sending the request the email here we get the response looks like it was successful and if I check the inbox now you can see that we got the email the sender is Lucy Lucy example.com and the recipient is John with the email body as we def find so let's imagine that we want to send this email to someone else let's try someone else uh let's say the recipient is under wo and the email is Anderson and Sun the problem here is that we have a hardcoded HTML and this is the use case for the template we talked about where we can have some placeholder like placeholder here like name and then when we send the email it's going to be substituted automatically by the way for this test let me test the case when we don't Supply the from address is going to use the default we have here which is info@ example.com so I'm going to send the email again and then to inbox if I refresh you can see we got the email from info@ example.com estimate and the recipient is Anderson and the body read John number lucky number 22 and a million so talking about substituting these values programmatically what I'm going to do is to use this answer from stock overflow I'm going to copy this function and modify it according to my use case so I'll copy the function and let me go to the mailer service and paste here we don't need this document. getet element something stuff I'm going to replace this with some values that we come as parameter we take HTML string so this is going to be HTML string then for data that is The Replacements let me just call it replacement this is going to be record object of key string value string so here I'm going to say replacement and replacement for this annoying stuff now I can use this template here for HTML let me remove from here I'm going to say that HTML to be equal to if D do replacement if the replacement is applied that means we have some fields in that HTML that needs to be substituted for I'm going to say that HTML is this Comm on this template d. HTML and d. replacement otherwise we just say d. HTML nothing to replace now um let me just remove this it's fine I will go back to the controller let's try to use that replacement instead of hardcoding John here um we can have at request at body from the request body let's say body to be record string and string then this body is what we are going to supply as replacement to be the body then for the HTML I'm going to say hi um according to this template stuff we are using you have the replacement between the percentage signs like this if you want to change it to use craes you can see how you can do it here so I'm going to leave it at the percentage signs I will say hi name your lucky number is and I'll put number bar I'll just leave the the other ones right but you get the point then in the request body let's do another test this time we are going to supply the name this is going to be uh Mary Lucas and this is Jon and then the number let's say um 777 okay so let's try this and send a request now we check what we have in the inbox so that works you can see that it says hi Mary Lookers your lucky number is 777 okay guys this brings us to the end of this lesson I hope it makes sense until next time enjoy coding
Info
Channel: ZestMade
Views: 2,360
Rating: undefined out of 5
Keywords:
Id: Pu1YP5PZKFc
Channel Id: undefined
Length: 24min 4sec (1444 seconds)
Published: Mon Dec 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.