Send Mail with Nodemailer Using Node.js Backend

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone and welcome back to daily tuition in this video we are going to understand how to send mail using node mailer library in many applications you would like to send mail when the user logged in order a product or make any payments in that situation you would like to send an email to the user you can use a node Miller to do that so let's understand how to say in mail using node Miller with node.js so I'm going to first simply set up the backend application and install a library called node mirror so let's first set up the backend application I'm going to create a folder here mailer and open my terminal inside this terminal I'm going to first enter into this mailer directory and initialize this folder with npim package so I'm going to say npm init hyphen y so this will just initialize this folder and I'm going to have package.json file inside it just for that I'm going to install few libraries inside this project so I'm going to say npm iPhone install Express I'm using Express to create a backend server and then I'm going to say nodemon to restart the server every time we make changes in the server file so once we have these packages inside this project inside this node modules let's create a new file here with the name server.js and just back to the package.json get rid of this test command and create here a start command and here you have to say server.js and whenever you make changes inside the server.js file you have to restart the server so I'm using here node mon Library every time we make changes in This Server file let me save this file back to the server right here I'm gonna first set up the express application so what you have to do is you have to say here constant Express is equal to and we're going to require the express then I'm going to say constant f is equal to express we create a simple instance of the express application and then we're simply going to say here F Dot listen and listen to the port now right now I don't have any port so let's specify here 5000 the port name and then I'm going to specify here a callback function inside this callback we're simply going to say console.log and say server is started so this will create a simple backend save this open the terminal and here I'm going to say npm start we are going to execute this start command when I press enter this is going to start the node.js backend server that's simple right these are that let me just close this file because we are not using it now just for that let me just create this port variable right up here so we're gonna say constant Port equal to and then I'm going to specify here 5000 and then we specify this port variable right here now just for that if you have the environment variable inside your application then you can specify that involvement variable here so you can simply specify here process dot EnV and then your variable name and if you don't have that then back to your default value you can also say Here app dot use and specify the express to use the Json format and just for that you can create different routes for your application so here we simply going to say Here app dot use and we are going to create a routes file inside a dedicated folder so instead of creating a route inside this file let's create a new folder here and inside that we have a file called route dot JS and then we call this file inside the server so at the top right up here we say constant app route is equal to require and inside a single code we specify dot forward slash specify routes route dot JS make sure you specify extension with the file name this are that you call this app route inside this app.us but before that I'm going to specify the end point so we space over here forward slash API and then we specify here comma and say app route like this we're going to call this variable right here so whenever we need to access the route of the application we need to start the application routes with the API endpoint just for that once we have this route file let's back to that file and create different routes inside this application so to create a route we're simply going to say constant router we need router to create routes so we say here require and we first require the express and from the express we call a router function and you specify that to this variable and just start that right down here so it's simply going to say here router dot post we're going to first create the post HTTP request so we call here router dot post then specify the end point so we specify here forward slash user or slash then switch over here sign up now that's upon you you can specify your own endpoint here just for that as a second argument you specify your controller with request and response parameter something like this but instead of having your controller inside this about file let's create a new folder and create the controller inside that folder so inside this Miller inside this project we create a new folder called controller we create a new file with the name app controller.js and inside this we create controller for this route right down here we create a controller for the sign up so we say here constant is equal to and as you know inside a controller we need to call a parameter called request and response and from the sign up I'm simply going to return a response with Json so we say here response dot status 201 dot Json and I'm going to say sign up successfully these are that right down here go to export this function so you have to say module dot export is equal to and from this object I'm going to say sign up so now you can access this controller right inside your route file so save this back to your route file and at the top here you can say constant sign up is equal to and you call it double dot forward slash specify controller app controller dot j and as you know signup is not a default export from this file so we call that inside an object call this sign up right yeah instead of this function so when you call this endpoint is going to call this controller let's suppose that you have one more route so if I say here router.post and create one more endpoint for products well let's suppose if I have product with get Bill endpoint and if I create here a controller right inside this file if I say constant get bill is equal to request and response and then return a response so copy this specify that here and say get Bill successfully save this file back to the route then copy this get Bill import that right here and then specify that for this endpoint and now just for that make sure you export this get Bill from this controller just like this and now just for that make sure you export this router so specify here module dot export is equal to router so you're simply going to export this router and get that router inside the server file right here and specify that this middleware and now if we make a post request to these endpoints then you will get the response back from this controllers to make a request I'm using here thundercland extension I'm going to open this thunderclient to make a post request I'm going to click on this new request and then I'm going to specify my server address so I'm going to secure http localhost colon 5000 forward slash API and inside that API as you know we have a user sign up you copy that and specify that right here and don't forget to get rid of this as right from this HTTP because this is not a secure endpoint just for that select the post hdf request and then click on the send button and you click on send you can see you will get a result from your server sign up successfully and if you do the same for this product endpoint so if you copy this endpoint and then specify that right here and make a post request you will get get bills successfully you'll get the response from these controllers now what I can do is I can just copy this endpoint back to the server and here we can simply say we're going to specify the backtic operator and say here server is running on and I'm going to specify here the address and I'm going to get rid of this 5000 and then specify here my Port name so we space over here what so when I see the changes I'm gonna have my server address inside my console so I can just copy this URL and specify that right inside this address now let me show you how to send email using node Miller so I'm going to first stop the development server to send mail using your node application you have to install a library called node mailer you have to back to the browser and open a website called nodemiller.com and from here you can find more about this node Miller library node Miller is a module for node application to allow easy to kick email sending to install node Miller you have to copy this command and then specify that inside this terminal once you install this node Miller then just restart your server close this terminal and just back to your app controller now inside this controller from both these endpoints you can send mail you have to first import node Miller so I'm going to say here constant node mailer is equal to and say here require and require node mailer now once you add the node Miller you need to create a node Miller transporter if you scroll down you have a transporter to make an email using SMTP server you have to call a node mailer dot create transport now this is going to create a reusable transporter object using the default SMTP transport now before we move to the real email account let's set up all this code using this testing account so what you have to do is you have to generate SMTP service account from the Ethereal email you have to copy this command and specify that inside the signup right here now because we have here a wait we need to call here a sync we need to make this controller completely a synchronous so if you don't have any real mail account then you can use this statement just for now for testing we are using this testing account just out there we're going to copy this code and then specify that write down here now this statement is going to create a reusable transporter object using the default SMTP transport and using this transport object you can send a mail now that's upon you you can specify any into this transporter inside this node Miller dot create transport we have to specify a configuration object inside this object we are going to have the host the port name then we have the secure property and the user authentication here you have to specify the username and the user password because we are using the testing account we just generated the random username and password using this create s account and then specify that right inside this auth when we add the real Gmail account inside this app then we replace this account credentials with the real account because for now I'm going to specify everything as it is copy the statement this is going to send a mail with the defined transport object so I'm going to copy that and then specify that right here but what I'm going to do is instead of Transport dot send mail I'm going to cut this object right from here get rid of the statement and here I'm going to say let message is equal to and then specify this object and here we specify all the details of the email so we first specify from from where we're going to send the mail then we specify two where we want to send this mail then we specify the subject the text what we want inside the mail and the HTML just for that I'm going to copy this transporter specify that right here and then I'm going to call here dot send mail and we're going to send a mail using this transporter and then here you have to specify your mail object we call here message something like this if this is a successful promise then we're going to get this response inside this then that we call here then something like this and inside this then I'm going to say return this bonds dot status 201 dot Json and inside this Json I'm going to return a message you should receive an email and then if there is a problem if we get an error we catch that error inside this function and say return response dot status 500 and then I'm going to say here Json and this is an error so we call this error object and I'm going to get rid of this statement so we comment this statement save the changes back to the new request and here I'm going to specify this user sign about let me specify here user sign up like this and when I click on the send button oops I think I misspelled something yeah right here it's node mirror I just misspelled this save the changes and when I make a post request you can see I'm going to get here a message called you should receive an email but where you can see this email to preview your email you can just access an object from this response right from here if I call here info and if I grab this and if I see here message and after this message if I specify here info info Dot message ID then we can get the message ID inside this info property and if I want to get a preview then if I call here property called preview and if you want to see the preview of your mail then you can simply call here node mailer dot get test message URL and pass info object now let me save the changes back to the request and make a post request again when we make post request you should receive an email this is the info this is the ID of your mail and when you click and open this URL open this URL and if you open this URL you can see you're going to have your email this is your subject then you have your time the message ID and your email text now if you want to make changes in it then you can do that as well let's suppose if I get rid of this hello world and if I make successfully register with us if I send this and then I'm going to copy this and we're going to do the same for this HTML as well back to the request and click on the send button this is going to make a request and you'll get the URL here just get that URL and specify that yeah you can see you're going to have your response successfully register with us so you can send any email from this body you have to just specify the subject your text and using this transporter you can make an email now once you understand how to make a mail using the testing account let me show you how you can use the real Gmail account to send mail to the user so I'm going back to the second endpoint of this controller from this get bills we are going to send a mail using real Gmail account so I'm going to add here a command and say send mail from real Gmail account and this first endpoint is for send mail from testing account right inside this get bill right down here I'm going to say let config is equal to and I'm going to create this configuration object this one so I'm gonna just paste it by here first service which is Gmail and then you have to specify the auth property inside is odd you're going to have the user and the user password and then right down here you call let transporter is equal to node mailer dot create transport and then you specify this configuration option here this one and just for that you have to specify your email right inside this user property and then specify a password of your email right inside this password property now we don't have to specify your email password right inside this Pass Property what you have to do is you have to open your Google account and create a simple app and then specify that password right here so what I'm simply going to do is let me first create a file called EnV dot JS now that's upon you you can create dot EnV file inside the server file as well but I'm just going to create here env.js file and right here I'm going to say export and object and right from here I'm going to say module dot exports and we are going to export an object inside this object we are going to have two variables first is email of the user and second is the password and we're going to specify this email and password right here so at the top right up here I'm going to say constant in the object we call email and password is equal to and then we space over here require and inside this acquire we get double dot forward slash env.js and we specify this email right here and password goes here back to the env.js and here we specify the email and password now I'm using my email right here so I'm going to specify here my email and then I'm going to specify password of this email so to get your password just search for my account and click on Google account the first link this will open all the details of your email just click on security and from the security scroll down and when you scroll down here you can see you have here sign in into Google and white form here you have to enable to step verification once you enable it you can find the app password click on app password specify your real Gmail account password here once you successfully sign in you will have the windows something like this here you have your app passwords app password lets you sign in in your Google account from apps on device that don't support two-factor authentication so you have to create here the app password I already have one app password right here you can notice let's create one more let me select mail here right from this select app and then select the device I'm using Windows computer so I'm going to say here Windows computer and I'm going to click on this generate button when I click on this generate this is going to generate a new password you can notice here just copy this password and close this tab at your env.js and specify that password Here save this file back to the app controller as you know we are accessing this password right from this dot EnV and then specify that to this authentication so once we have the email and the password now we can send a mail using this Gmail account keep in mind when you download this project you have to create this dot EnV file and specify your own email and password instead of using the same technique to create an email I'm using mailgen library to generate a mail so I'm going to open my terminal stop the development server and I'm going to install a library call npm I mail Jam using mailchain we can specify the template to the email let me just start the development server once you have the mailgen at the top right up here I'm going to say constant mailgen is equal to require and here we call the mailchain library inside is get bill right down here once we initialize this transporter right down here I'm gonna say let Mill generator is equal to and create a new mailchain instance inside an object we need to First specify the theme property which is default then we need to specify the product which is going to be the name let me just add here a link of mailchain just after that let's use this mail generator just start that by down here let's generate the email so we say Here let response is equal to in the object we suppose specify the body of the male so we call here colon body we specify the name of the user then we specify the intro you get all these properties from the mail chain object please specify here intro then I'm going to specify here your bill as arrived then we specify the table we create a table inside a mail and we specify data to it inside an array we specify the item which is going to be node Miller stack then we space over the description of it so we specify here description which is going to be command a back-end stack application and then we specify here price is going to be 10. now just for that we need to wrap this inside an object something like this if you want you can add one more item inside this ad as well that's upon you you just have to copy this object and add one more item I'm going to leave everything as it is and just out of that just out of this table you can add the outro and here you specify looking forward to do more business now once we have the body of the mail you have to specify this response to the mail generator so we have to specify here let male is equal to mail generator dot generate and you have to specify this response right here this one so we call this mail generator and then call generate method of it and call this response object so this will generate a mail inside this mail variable and after that right down here you can simply specify let message is equal to and here you specify from and you specify your own email here just copy this and then specify that here then specify here 2 where you want to send your email we're gonna get the email from the user so at the top let me just minimize this I'm going to get the email from the user so I'm going to say here constant in the object we say user email and we get that from request Dot body so when we make a post request we need to specify where we want to send this mail so we specify that user email right inside this variable and we call this user email right here these are that we specify the subject subject is going to be place order and then we specify the HTML which is going to be this male variable this one and just out of that right down here you have to call your transporter this object dot send mail and here you have to specify this message then call then and from then I'm just going to Simply return response dot status 201 and then say here dot Json and inside the Json I'm going to send a message you should receive an email I don't have to send any email URL or something like that these are that here I'm gonna specify catch if there is an error then catch that and return response dot status 500 dot Json and inside it we return error get it off this set map so I'm going to comment this back to the new request and here you have to specify this route product get Bill you specify that right here this is a type of post request and you also need to specify the user email as you know if you scroll up then you will notice here you have this user email property you have to pass this value when you make a post request so what we are going to do is we select the body and in the double code we specify this user email something like this and then right here we need to specify the user email here now instead of specifying the real email here you can just get your email in the random email as well so what you have to do is you have to open the temptmail.org and copy this random email just copy it this is a temporary email address you can get your email right inside this inbox so just copy this email specify that here and when you click on this send button oops something is missing right here yeah I don't have name so let me specify value here daily tuition save the changes let me click on the send button and make a post request I'm gonna get the status 201 and I'm gonna have your message call you should receive an email if I back to my themed mail then you will notice here we have here an email from the sender daily Division tutorials.gmail.com if I open it I'm gonna have my mail here this is my name then you will have this text and your table and if you back to your Gmail and if you click on send then you'll notice you have here a send mail so we are sending this email from this real Gmail account to this temporary mail account now here I'm using the temporary email you can replace this value with your friends email and make a request your friend should receive an email from you with this email body so I hope you understand how to make mail using node Miller using node.js you can download this code from the link provided in the description if you find anything useful make sure to press the like button share this video with your friends subscribe for more latest videos that is all for now I will see you in the next one [Music] thank you [Music]
Info
Channel: Daily Tuition
Views: 26,957
Rating: undefined out of 5
Keywords: Send Mail with Nodemailer Using Node.js Backend, nodemailer, send mail with nodemailer, send mail with node backend, send mail with node.js, node.js tutorial for sending mail, send mail, send mail with gmail, nodejs, node tutorial, node example, node js, node with tutorial, express node.js backend, node tutorial with backend
Id: lBRnLXwjLw0
Channel Id: undefined
Length: 28min 29sec (1709 seconds)
Published: Wed Dec 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.