Sending Emails in Node.js with Nodemailer & Gmail | Step-by-Step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to the channel in the previous video we have seen how to send an email using send grid module so in this video we will see how to send an email using node mailer module node mailer is a popular module in node.js for sending emails also I will be using Gmail service for my SMTP mail service provider so basically node mailer communicates with the Gmail SMTP service provider this service provider will take care of sending the mail from sender address to the recipient mail address let's get started with code so I have created a project folder in vs code with a couple of files so I will use environment variables in this project also I have one image file as well as one PDF file so which I will use these files as an attachment to my mail also I have created one empty file sending email dot JS so here onwards we will start with the coding part so before that we need to install some packages so let's get back to the browser let's copy this command to install node node mailer so I have opened my Powershell uh on the project folder so along with node mailer I will also you install dot EnV for handling environment variables all right let's go back to vs code so first of all Let's uh import the library also we need to import uh dot EnV so that we can access environment variables inside this project and we need to call the config method so that it will attach environment variables in the dot EnV file to the system variables okay let's go back to browser so if you go to nodemailer.com so you will be able to navigate to the site so once we install this uh module if you scroll a bit down so we can see the example code so let's copy this code so first of all we need to create a transporter so let me change this variable to cons so here I will be using as mentioned like the service will be Gmail since we are using Gmail uh so the host will be smtp.gmail.com so if you are using any other mail service provider then you need to provide the that particular host name here so also if you ignore this port number by default it will take 587 and like in case if you are using port number 465 like this uh then in that case you should use secure as a true so let me change back I will come to this port numbers uh in a moment so let's update the user details so here you can see we need to add authentication details so that node mailer can communicate with the Gmail service provider so like I will use my sender mail address so from environment so I have a variable called a user and this will be the app password so let me call that variable as well so I have created one password called app password let me remove this uh or let me update this so this will be app password from email account so again I will come back to why we are using this app password so let's continue with the further code so once we created our transporter we need to so create these options so let me copy this so I will create a variable called mail options so here you can observe this is the from address like basically there are different formats that you can apply your from address so this is one way like you are providing the name as well as your RAM address in this way also you can observe you can simply put your address directly like this so I will update this thing so that we can have a a name so here I am naming the name as uh the channel name and the second thing is the address property so this is uh the sender address so again I will be using this same environment variable okay so now so I will update this uh two address to one of my mail address so in case like if you want to send this mail to multiple peoples then you can simply keep it in a array of address like this or you can also do uh comma separated values like uh uh one more mail like this but uh I I just follow the array of addresses for the simplicity all right so the next thing is subject so I will update this to so send email using node mail and Gmail let's give this a text and HTML like this so just in case like if you have uh if you want to mention Cc or BCC then again uh these are the you can provide as a list of values in Array format but uh in this case in my case like I'm not going with that so you can go with your requirements so next case is like uh so as I mentioned like previously like so we have an image file as well as a PDF file so we will send this as attachments so we need to create a property so this is a array of objects where each object represents the attachment so first of all we need to provide the file name give that PDF file name and the second thing is we need to provide the path where the file is located in our project so I will be using a built-in module path module so let me import that so using path module like we can join the directory along with the file name um using test.pdf uh also like you can provide the content type so here it is a PDF right so we can go with application flash PDF uh it is it is not a mandatory but uh if you provide it it is uh better so you can also give a try to send without the content type also so it's completely optional so the next thing I want to attach is the image file so I'm just copying this so let's update the names all right so we are done with the mail options so we have created the transporter node mailer transporter so this is responsible for communicating with our Gmail service from mail service provider and then we have created our mail options so here we have mentioned our from address uh to address to the list of receivers and the subject text and HTML version also we have provided our attachments uh okay so now I would like to create a function here which is a send mail so this is an async function so in this function like let me open this so I'm just consoling the error so here I will call so I will make use of the transporter and then I will call the function send mail so this will accept this mail options so first of all when we are calling the function so we need to provide the transporter as well as mail options to the function so that we can make use of them inside this so just trying to implement the functional programming so inside this we need to provide the mail options all right so after this line I will console log so this determines the mail has been sent successfully okay so now our code part is completed so let's go back to browser and look at how to generate a password from our Gmail account so this is my sender Gmail account so if you go to your account you need to go to you know manage your Google account so here onwards you need to go to security so if you scroll down so first of all you need to enable two-step verification here so under a sign in to Google like you need to enable this so once you complete this one if you click on this so once you go inside the two-step verification you can find if you scroll a bit down you can find the app passwords here so I have already created one so you need to create one so if you go inside it again so you can see I have created one for this tutorial so what you need to do is you need to select the app so you can go select the others you can provide your node application name node project as per your project name and then you can click on generator so it will create a new password so it will create a 16 digit password secret password so do not share with anyone so you can use that password in your authentication data here and I have play already placed in my environment variable in dot EnV file so previously what happened was if I go back again so let me go back so as you can see like uh if you try to search with the less keyboard you can see blast secure apps and your Google account so if you go to this article so you can read this like you know from me last year 2022 Google is no longer supporting the use of third party uh apps or devices which ask you to sign in to your Google account using username and password so before we were actually uh you know enabling the less secure apps in our Google account and here we are providing uh you our sender Gmail address and for the password like we are providing our Gmail password so this is actually kind of uh security Bridge like you are providing your Gmail account data to the third party apps which is not secure so uh they have uh you know they are no longer supporting this now as an alternative approach like they have come up with a passwords so I hope you understand the difference you know why we need to generate a app password from our security Google account so let me close this so I believe we have a code setup and password is generated so let me execute this and verify all right so I have received this console log email has been sent so let me verify from our so let's go to the recipient mail address first of all so let me refresh this okay you can see uh so the name is web Wizard and it has a subject so this is what uh why you know I'm using the from so this is the name that it is showing otherwise it will use our email okay if you go inside so you can see this is that uh HTML tag and then these are the two attachments that we have attached uh just in case like if it went to Pam folder just verify it also but most probably uh if you provide a proper subject with the data proper data that doesn't seem like a spammy then it will come to your inbox uh properly so let me go back to the code base so here like initially like we have spoken about the port numbers right so by default like if you do not mention like you you are not needed to mention this like you can simply ignore this thing like this so default Port that it will go is 587 so if I go to the SMTP transport the fourth uh section so here if you see the port the general options Port so it is the port to connect to default to 587 okay if it is a secure is false so that's what uh you know so these are the default values so for when you are using in any port other than 465 Security will be false so what is the use of 587 port number so first of all if we understand this so you can see originally the SMTP Port is uh 25 but later on uh today like uh you know we are using Port 587 so this is the default Port nowadays so this is the port for encrypted mail Transmissions using the SMTP secure okay so you can also observe the difference like Port 465 is also used sometimes but this is an outdated implementation and Port should be used if possible so most probably your service providers will support Port 587 so just in case if you have a problem with the using 587 and you are supposed to use 465 then so here when you specify 465 you you are supposed to change this to true that's it so I'm just going with the default options also uh one of the article that I found on the internet so uh you can see SMTP Port 465 it is a deprecated port and you should no longer use and if if you look at this port 587 the right choice for every situation so like if you try to understand this so it is a modern SMTP port for message delivery so natively like by default it supports the TLs security mechanism allowing for the secure submission of mailover smdp that's all about the video guys uh I hope the video is helpful do like and subscribe to the channel let's meet in another video Until then take care bye
Info
Channel: Web Wizard
Views: 1,279
Rating: undefined out of 5
Keywords:
Id: QDIOBsMBEI0
Channel Id: undefined
Length: 14min 15sec (855 seconds)
Published: Sat Jun 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.