Send Emails in Express.js with Nodemailer SMTP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this Express JS tutorial we are going to learn how to send emails using node mailer so let me go ahead and initialize a new project say npm init Dy then I will install Express npm install Express because I want to keep the credentials for for the email in a EMV file let me also install M that will help us to manage that then I'll will hit enter I will also add node more as a Dev dependency this is for autor restarting the server when we make changes in the code so I'm going to say npm install as Dev dependency node Monon all right so let me open the project in vs code and I will go ahead to add two files One Call server.js and the other one called app.js in app.js first I'm going to require Express require to bring in Express Express and we have an instance of the app so con app equals Express and finally I will just modu do export I will export up then in server. J I'm going to uh say app. listing app imported from the from the other file listing we provide a port con for to be equal to 3,000 so listing on P 3,000 let me just put a console.log console log list on Port and then the port then for starting the server we go to package.json and add a command let's call this start or let me call it Dev and here I'm going to say no more and just point to the file server.js okay I'll go back to the termin now and say npm run Dev which Dev is the is the command we just added and we get listenting on Port 3,000 I'll stop the server now now and go back to app.js I want to add one route so we can send a request and the email is sent out so going to say app. post this is going to be a post request to endpoint let's call that send email oh my and as usual we have request response so I'm going to keep this very simple no uh validations and all those things so we can focus on just learning how to send email I'm going to as expect that the request body we have uh let's say name name and email these are going to be the this is going to be the recipient of this email that we are going to send uh let me put the codee for sending email in a different file let me call this mail. J so here we make use of um node mailer which is something we have not installed so let me go ahead and install node mailer npm install node mailer npm install node mailer with node mailer installed I'll go back to this uh may. js5 and bring in node mailer node mailer equals require node mailer then we create what is called transport so let me call this transport to be equal to node maer do create transport this will have host process. EMV dot let's call this mail poost I we copy and paste and change the keys and then Port we are going to put these values in the environment variable so mail port and whether this is secure should be secure but because we are let me just turn it off because I'm in the local environment so this will be false that is whether to use SSL for the email and then we have OD so this is going to take two Fields the user the username and password so let's call this one M user and then we have pass word next thing I want to do is to have these environment variables in a EMV file so I will create a file called EMV and copy over those values the host import the user and password but to read the values we are going to have here in the application we need to make use of uh the M that we installed earlier so I'll go to this server.js file I'm going to say require M and do config config so this we expect that we have a file named EMV at the root of the project with this import like this this configuration like this now how do we get the values we are going to put here these are going to be your own specific values depending on the email service you are using so whether you're using Google service Z AWS whatever you just get the SMTP credentials and put over here for this demonstration I'm going to make use of ma trap which will give me a free testing credentials so over to met trap this is my met trap I will copy the the username and the password The Host and finally the port so I don't know let me try 465 then I will go back to app.js here I'm going to okay before this let me go to M.J and have a function for sending the email const send yeah send email to be equal to this is going to take the recipient that is the email address to receive the email recipient and subject of the email and then the main message then inside here I want to have make use of the m transport above so I wait transport. send email and we pass from this is going to be the sender you can configure this in your EMV let me actually have it my EMV or it can be dynamic you can pass it as parameter so let's say no reply at example.com and then two this is going to be the recipient then we have the subject we already have that guy there and the text the message in plain text also we are going to have the message in HTML format but I'm going to use this guy for both of them so in your own applic you want to supply HTML here and just a plain text version of it to text so I'll leave everything like this I'm getting this error because to use a weight this function has to be snc um let me also go ahead and return here the result finally I want to modu do export let me export the function send mail so I call this recipients because you can send to more than one emails at the same time now I can go back to appj and make use of that function send email make sure to import that so this is expecting the recipient the subject and message for the subject I mean you can can be whatever you want it to be subject to be equal to Welcome to our website whatever then for message uh to be equal to that and finally the recipient con reent I'm going to send to just one recipient let me format that a bit so I will have the name of the recipient and then the email address you can just pass the email address if you want to and inside here email now we can give this a try by sending email to Local Host 3000 SL send email so right I'm putting Postman as my client I will call that endpoint it is a post request in the request body we are going to have the name uh let's say John do and the email so let me have john. do at example do come and send the request oh this error is because Express is having issue um dealing with the Jon data in the request so let's go ahead and tell Express to handle the Jon data we say up. use express do Json back to postman I will send the request again so we are stuck but if I go to the browser you will notice that I got an email few seconds ago and this is the recipient the sender the recipient that is John do and this is the email body we are stuck because something that is well known about Express we did not send any response in this route Handler so the solution is simple I'm going to say I may before sending this email uh let me should do some error handling as well then result I don't know what to do with the result. cach error I will leave that as well I'll say raise do send or raise. Json come on message say sending email in a moment okay so I will give this another try I will send the let me send to someone else uh Lucy I'll send to Lucy this time and I get the response sending email in a moment then when I check the inbox you can see that we got the email this time it is for Lucy although the email address was not changed so guys this is how you can this a basic example of how you can send emails in your expressjs application using node mailer SMTP until next time enjoy coding
Info
Channel: ZestMade
Views: 1,268
Rating: undefined out of 5
Keywords:
Id: PJmz0GhE45s
Channel Id: undefined
Length: 14min 54sec (894 seconds)
Published: Sun Mar 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.