How to send email using Gmail API (uses OAuth2 and nodemailer)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay guys so welcome back again so in this video let's see that how do we send emails using the gmail api and for that we'll be using or2 and we'll be using the npm package that is called nodemiller to send our emails so the first thing which we need to do is to go to console.cloud.google.com and i'll provide all these links in the description below so once you open this page that is if you haven't registered on google then you should register on google and then you should log in with your account and then you can simply go to console.cloud.google.com so once you are inside your console and if you have not created any of your project before then you won't be seeing this in screen but instead you'll be seeing that you should create a new project so why not let's create a new project for this tutorial so let's click here and let's select and let's select this new project here so let's create a new project so let's click here and here we need to provide in the project name so for this tutorial i'll be calling it gmail api tutorial and for the location we are not selecting any location and now let's simply click create here let's click create and now we need to select this project that we have just created that is the gmail api tutorial and it is currently being building so let it build for a moment and now let's select this project here and now what we need to do we need to go to apis and services and here inside this tab we need to go to the dashboard here of the apis and services so let's click here and now we are inside the api and services dashboard and now what we need to do the first thing which we need to do is to create a consent screen so let's go here that is what consent screen since we are using our two so let's click here that is over consent screen and here for the user type we need to choose external so let's click here and let's click on create here and this is only for your internal purposes no one else is going to see that and here we need to provide in the app name so let's provide it here that is gmail api tutorial so like this gmail api tutorial and here we need to provide in the support email so let's provide our support email that is from the account from which we have logged in and i would highly suggest that you should not choose an app logo for your application because if you choose an app logo then you need to submit your application to google for verification and it would take a couple of days so i would suggest that you should not do so and now let's go here at the very bottom and here we need to provide in the developer contact information so we can provide our email here that is the current email so yours truly 2607 at gmail.com like this and now let's click on save and continue and here for the scopes you can leave everything at its default value and let's click on save and continue and again for the optional info we can simply click save and continue we are not going to provide any optional info here and now this is the summary and here you can simply go back to the dashboard thing here and now your oauth consent screen has been set up so now let's go to this credentials tab here and here we need to create an oauth client id because currently we do not have any over 2 client id so let's create one so let's click on create credentials and here we need to select on oauth client id and for application type we can select web application like this and let the name be web web client one and here one thing which we need to provide is the redirect url so let me provide it here and the redirect uri would be this url let me copy and paste it here so let's copy it and let's paste it here that is developers.google.com forward slash oauth playground and make sure that you do not put a forward slash at the very end of this uri so just make sure of that because if you do so then it would result in an error and you do not want any error so just make sure that you do not add a forward slash here after oauth playground and now let's click on create here like this and now our oauth client is created and we have the client id and the client secret so let's click on ok here and let's open this thing here that is web client one which we have just created so here we see that we have the client id the client secret and the creation date and the authorized redirect uri so now let's go to this uri that is oauth playground that is the developers.google.com forward slash oauth playground and here what we need to do we need to authorize our api that is we want to use gmail api so here we can put in our scope that would be https forward slash mail.google.com and here we need to select this gear icon and here leave everything at its default value but we need to provide our own over credentials so let's check here and here we need to provide in the client id and the client secret so let's copy the client id from here and let's paste it here and for the client secret let's again copy and paste the client secret so let's copy it in full and let's paste it here and now we can simply click close here like this and now we can select this button here that is authorized api that is you want to authorize mail.google.com so let's click here authorize apis and now it asks us that from which account we want to continue to this application that is gmail api tutorial application that we have created inside our cloud platform so we are going to select this email that is our email here so let's select this email that is your australia2607 gmail.com and here it says that this app isn't verified and that is very true because google hasn't verified our app but still we can continue by clicking on this advanced tab here and here we can simply click here that is go to gmail api tutorial and it now it asks us for permission for this application to read compose and send messages from our gmail so let's click on allow here and now we need to again click on allow here and once we do so we get back an authorization code here that is this authorization code but here we need to get the refresh token and the access token so what we can do we can exchange this authorization code for refresh token and access token so let's simply click here that is exchange authorization code for tokens and now we have the refresh token and the access token and if and if we again want to see the refresh and access token we can simply click this step 2 and here we have the refresh token and the access token so now let's do some coding so let's go to our terminal here and here if we see that i've created this new directory that is gmail api and if we list the files that is it is an empty directory so let's create an npm project inside it so npm init hyphen y to keep all the defaults and if we now list we see that the package.json has been created so now let's install a couple of dependencies here so npm install google apis and this dependency is for getting the or2 client and the second dependency would be node mailer which would be used to send emails like this so let's install them and let's wait for a couple of seconds and now let's go to our vs code and here we see that we have these uh this package.json file and we have these dependencies that is google apis and the nodemail dependency so now let's create a new file that would be our application file so let's touch app.js and here we have this app.js file and here what we need to do we need to require node mailer and we need to require google from google apis so const node miller equal to require node mailer like this and const google we can use destructuring to get google from google apis equal to require google apis like this and now we need to create a couple of constants here that is the constants for or2 tokens so client id equal to something and we'll be filling in the values later on const client secret equal to something and then const redirect uri this would again be something and we'll be filling in the values in a moment and const refresh token equal to something so now let's quickly fill in the values here so let's get the client id from this google thing here that is the application we just created so let's copy the client id let's paste it here let's copy the client secret and let's paste it here and now let's copy the redirect uri so let's copy it and let's paste it here again and now let's also grab the refresh token that we have just generated so let's copy this refresh token and let's paste it here like this and now all the constants are ready and now what we want to do and one thing more that we are not going to use this access token because this access token is only valid for 3600 seconds and 200 seconds have been passed and this access token would only be valid for this thing here that is this much seconds here but instead what we should be doing inside our application that is we are going to generate access token on the fly that is as and when required so let's go back to our application here and now we need to create our or2 client here so const oh auth to client equal to new google dot auth dot oot2 like this and here we need to pass in some values here that is the client id the client secret and the redirect uri like this and now we need to set some credentials on this or2 client so let's set it so or took line dot set credentials and here we can pass in an object containing the refresh token that we have just copied and pasted and it would be this refresh token like this and now let me create a function to send email so function send mail this and here what you can do you can pass in a subject of the email the body of the email and so on and so forth that is to whom you want to send an email but for all those values i would be hardcoding it inside the function itself and one thing more since i want to use a weight inside this function let's make this function async here so let's make this function async like this and now since we are using an async function let's do each and everything inside try and catch like this and if there is an error we can simply return the error that is return error like this and now let's do something inside the try block let me make the font size a little bit larger so the first thing which you want to do is to get the access token from our or2 client so that is pretty simple to do so cost access token equal to await or stock line dot get access token like this and now what we need to do we need to use node mailer to create a transport that would be used for sending emails so const transport equal to node mailer dot create transport like this and here we need to pass in an object here and the object would contain some key value pairs firstly the service the service would be gmail because we are using gmail for sending emails like this and then we need to provide in an auth object like this and it would again contain some key value pairs so firstly it would contain a type key which would be oauth 2 so oauth 2 like this and then we need to pass in the user so the user that is the user we have authorized for our application and the user was yours truly 2607 at gmail.com like this and then we need to provide in the client id so client id would be client id client secret would be the client secret like this and this should not be equal to it should be colon here and then we need to provide in the refresh token and the refresh token would be refresh token and the access token would be the access token that we have just generated it could be this access token that is this access token which we have generated and now this transport is pretty much done and now what we can do we can use this transport to send our email and before sending the email we need to create some options for the email so let's create them so const mail options equal to an object here and the first thing which we need to provide here is the from email address like this and here either we can simply provide in our email address that is yours truly 2607 gmail.com or what we can do we can also provide in the name of the sender dynamically here so let's say yours truly 267 and here we can use an emoji here like this email emoji here and then in the angle brackets we can provide in the sender email address that is the from email address and then to whom we want to send an email so let's send it to pick your page at gmail.com uh where am i yes here it is so it should be gmail.com not only gmail and then we need to provide in the subject of the email so subject would be hello from gmail using api something like that and then we need to provide in the text version of the email so the text version would be hello from gmail email using api something like this and then optionally you can provide them the html version of your email i would highly recommend that you always provide in the text version and the html version is always optional because some clients do not support html emails so this would be again let's copy this text here and let's paste it here and let's use h1 tags here simply to make this in html like this and now what we can do we can simply use the transport to send our email so we can say const result equal to transport dot send mail and here we can pass in the mail option that we just created and since this returns as a promise so we can simply await here that is we can simply use a weight here and then we can simply return the result from this try block here like this and now we can call this send mail function here using promises here so send mail dot then and here we would be having result so here we can simply console.log the result that is email is sent and optionally we can also print out the result that is coming from the this this thing here that is from this send mail here and now let's also catch the error if there are any so catch error like this and here we can simply console console.log the error will be error dot message like this and now what we can do we can simply run app.js but before running app.js i just noticed here that this is not the email address that i have in control it should be pick your page at gmail.com so now let's see if this app.js and now let's run node.js so note app.js like this so let's press enter and it will take a couple of seconds to send an email so we see that the email has been sent to this email address and now let's open our browser here and here we are inside the yours truly 267 gmail.com and if we see that we got a security alert from google that is this application was granted access to our google account so that is very true because we did it so let's go back and let's open pick your page at gmail.com and here we see that we have a new email from yours truly 267 gmail.com so let's open this email here and here we see the html version of the email the sender name is this name that we provided on the fly and the sender email address is the this email address and it was sent to us here and this is the subject of the email and if you open yours truly to 607 gmail.com and if we look at at the send folder here so let's open the send to folder and here we see that we send this email to pick your page gmail.com so this is how easy it is to send emails using the gmail api so if you have liked the video do hit the like button and if you haven't subscribed to the channel do subscribe to the channel and if you want you can support me on patreon or paypal.com i'll have both the links in the description below so thank you bye bye tata take care and have a good day
Info
Channel: yoursTRULY
Views: 35,961
Rating: 4.9516616 out of 5
Keywords: yourstruly, using gmail with oauth2, oauth2 tutorial, using gmail with nodemailer, how to send emails from gmail, how to use gmail with nodemailer, gmail api
Id: -rcRf7yswfM
Channel Id: undefined
Length: 17min 55sec (1075 seconds)
Published: Mon Nov 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.