Complete Guide: Sending Emails in Node.js with Amazon SES and Gmail | Step-by-Step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the channel in this video we will learn how to use Amazon SCS service for sending emails in node.js applications so basically Amazon SCS is a simple email service and a cloud-based email solution so let's get started with the video first of all I have already logged into the account as a root user so you need to if you go to the Amazon console so you need to create a account and then if you sign in you will take back to the console home page so before starting with Amazon SCS like we need to create a user so you need to search for IAM service thank you so we need to create a user so that we can take the credentials of the user from our node.js application and communicate with the Amazon services so if if we go to users Tab and here we can create a new user so I'm creating an user for this tutorial so we don't need to enable this because we are gonna have a programmatic access so we here also it is saying if you are creating programmatic you can also create this you can provide this uh access after creating this user so I'm just clicking on next okay so next thing is uh we need to attach policy so here we are looking for SCS full access so if you search for this one so this is the one we need okay you can click on next okay let's uh create the user all right so we have created a im user with the SCS permissions so now if you go to if you click on this user now uh you can see much more information about the user so here what we are supposed to do is we need to create the credentials so you need to go to security credentials tab and here if you scroll a bit down uh you can see uh access keys so we need to create one so we create access key so you uh for different use cases like you can select the option so uh in our case let's go with the other options so it is just a indication that you know make sure you are storing in a secure place and not showing to the public and all okay let's go to the next step create access key okay so now uh we have created the access key now we have to store these Keys as environment variables in our node.js application so I will create variables and store these values and then come back again all right so I have added the environment variables in our project so let's click on done okay like you can also download the file with this uh access key and the secrets go all right so we are good with the user part so now onwards like let's go back to scr service so I'm you can search for SCS Amazon simple email service select this one so this is the home page right uh so first of all we need to create a identity like uh you can see we need to create and verify a sender identity so this identity can be you know using your domain or a email address if you have one so that we can send email through Amazon SCS so if you have a domain then you can configure a new email from that domain so that you can send emails using that also you can if you have a regular email then also you can go ahead and proceed so let's go ahead and create identity so you can see uh it is providing the two options so in this case I am going with the email address so I need to provide the email which I'm going to use for this tutorial okay let's uh click on create identity so you can see uh so identity status currently it is pending so what will happen is uh it will send an email for the verification with a link so we need to click on that so that it will verify so let's refresh this so you can say I have received a email from AWS so you can see it is asking me to address this email for Amazon SES so let's click on this link okay so we we have verified our email address so let me go back and refresh this page all right so our email address is also uh verified so we have created a identity so let's uh go back to our project uh okay let's open vs code so I have already created a project folder and I have created a DOT EnV file so in this file I have added my access key and secret access key and also the sender email address which I have used uh which I have verified from AWS right let's install a couple of dependencies uh here so you can see so this is the first command that I have executed for creating this project so this will create a package.json with the default options so for this tutorial let's install AWS SDK as well as a DOT EnV all right so these two packages were installed let's clear this screen so on in our project so let's create a file called send email so let's start with the importing the packages so I have imported the AWS SDK which we have installed and then uh this is the line for accessing environment variables in our project okay so the next thing is uh so we so I have created an object that contains a access key ID secret key as well as the region so to get this uh like I have already shown uh you know where we have got this information so for the region so like if you go to your console So currently you can see I'm inside Mumbai so if you click on this drop down so it will show in which region and you know the code for it so far for me it is AP South one so this is my region value which we need to use in our environment okay now we have to create an instance for this uh so here you can observe like using the AWS SDK so we are accessing the SCS service by passing this uh configuration object with this information so now I would like to create a function naming send email so this is an async function so this will receive a two parameters like uh recipient email and name so this is uh like you need to modify the function so that it matches your requirements so so the recipient email here is uh the same email I have used for the verification and the name I'm using the channel name here you might ask why uh we are using the same address for uh you know Center and recipient mail address because uh if you go back to the dashboard like you can click on Account dashboard so you can see your Amazon SCS account is in sandbox so in sandbox environment you can use all the features but there are certain limitations and restrictions are there so when you move out of the sandbox you can increase your limit so for that you need to raise a request for production access also you can see uh for me it is giving me 200 emails here for 24 hours period so this can you can increase this limit by raising your production request also the note point is that you know you can only send mails to the verified emails so here the sender email address is verified so you cannot send outside of that so that's the reason like you know you are supposed to move out of the 10 box so when you are in sandbox you are supposed to only send emails to the verified emails so let's get back to our code so first of all we need to provide the params like so you can see there are some options here so the source email address so this is the AWS SCS sender email address that we have verified so this I am taking from the environment variable and here on the destination side up so this is an object so that accepts the two addresses property so the here we are mentioning the recipient emails so this can be multiple emails if you want so again uh So currently I have no reply to addresses so I'm just keeping it empty so on the message section like you can expand so on the body side you can see I'm providing the HTML as well as a text versions like initially it will render the HTML version if it having any problems then it will fall back to the text version so here you can see the character set what type of encoding you are supposed to use so utf-8 so this is the data so this is the HTML that it will use in the email body so again for the text also it is using the same properties and again so we need a subject right so again it follows the same properties but we you can see I'm going with the simple information so hello name so here you can ask so this is why I'm using the name like in the subject like I want to print the name so whichever information I would like to pass so that's why I'm taking name from the function call and that is where I am passing here okay so next thing is so we have created a params object so now so let me open the tricas block so if we encounter any errors we can go ahead and print out so inside try block like I want to add a two lines so here you can see uh I'm using the AWS SCS object so here this is from this place like we are using this one so from this uh object like I'm calling send mail method and here we are passing the params so these are the main options that we have created so this will return so we need to make it as a promise so that we can handle it so I'm using the SNK weight approach here so that's why I'm using debate so now if the mail sends successfully then it will go ahead and execute this line okay so let's uh verify uh sorry save this file so let's go ahead and try to run this uh execution okay you can see it has given some note warning they are planning to enter you know JavaScript version 2 into the maintenance mode in 2023 so they are suggesting us to migrate our code to JavaScript SDK version three okay so I will provide some solutions how to handle these things so you can observe like the email has been sent so we are getting this response object as well so let me go back to browser and verify whether we have received the mail foreign new mail so let me open this okay so let's uh I mean uh if you provide proper content I hope this won't become a spammy so I'm just okay so this is the text body and uh this is the subject we have used you can see the name was properly rendering here okay let's go back to the code base okay so uh currently If You observe like in package.jsons uh AWS SDK so we are in the version 2.1395 okay there are some alternative solutions for this so to handle it uh one thing is like you can suppress this warning still being in version two so for example I'm just adding one more line here so you can see uh so from this SDK Library maintenance mode message I'm trying to suppress it to True by enabling this so if I save this one so I'm just going back to the tunnel again I'm going to send the mail again so you can see that warning message has been suppressed and we can see the expected message again sorry we received the message again all right so now we we are supposed to you know update our code base writer so that we will make use of version three so if I go back to browser again so you can find this uh article on Amazon so I will share these links uh so you you can see that you know Amazon is making use of modular approach in AWS SDK why it is so the motivation behind it is like you can see in the version 2 it is the entire single package right so when you when we import this one inside our application so even though we are using a subset of functionalities like uh SCS S3 bucket or some other services so you can see there are some performance implications uh right where uh having a big code base is a problem so you can reduce the size of the of your application by eliminating the that code like some modules which we are not using so we don't need those things so we can get rid of them so previously like you can see this is how you are importing the entire package right so in in version three and all so you can make use of them like into you can import the individual packages whichever is required so I'm just going to install install this package now so I will show the solution how to make use of them okay let me open the windows so I'm going to install that so as a separate module so let me press enter all right so the package is installed like again so I'm going back to package.json so you can see now currently we are using this uh client individual components so which is a version 3.3 so it is up to you as per your requirement whether you want to proceed with this uploads or this one so I'm just providing the solutions on how to use the modular approach okay now so I'm going to create a new file so version 3 we have to import again the library so from AWS SDK so we are importing the client SCS so this is related for uh email service so here you can see I'm importing the client as well as email commands and email command so again this is referred to the environment so again our configuration so here you can observe like the difference is that in the in this configuration object like I have a individual properties for the credentials and the region so previously we have these three properties in same object but in while using this uh client SDK so you are supposed to follow this approach so I have placed a access key and secret key inside credentials and the region outside it okay so now we need to create the SCS object service object using that client Constructor okay so now you can see I'm passing this configuration to this client and we are initiating an object uh instantiating an object so again I'm going to create a new function and we need to so again uh so let me take this line again foreign here you can see we need to provide the mail options so in the params object like you can see again we have a source we have this destination which is similar to previously so this these things are same like these are common so again I am opening one more try cash block so inside this try block uh let me so here you can see uh we need to first of all pass this uh options to the send mail command which we have imported from this SDK and this object we are supposed to pass to the client we have created so using the client we need to call the send method and to that we need to pass this email command okay so we have provided our mail options and we are creating the email command send command and from the using the SCS client we are calling the sun command okay let me save this and go back to the terminal so this time we will execute this version 3 approach okay so I'm executing this okay it seems like we are encountering an error let's see oh I'm sorry uh actually we have installed a wrong package here it's okay let me go back so we need to uninstall this I'm really sorry uh so we are supposed to install this uh client SCS which is related to email service not the S3 module okay so now you can say I have uninstalled the previous one and we have now SCS 3.3 version okay I think now we are good to execute this okay let me go back okay now I can see one more mail which is coming all right so initially we have used the AWS SDK like uh using this approach like we were encountering some warning maintenance mode right so in order to fix that we have used this suppress command and the other alternative solution is to migrate to version 3 using this approach modular approach okay so the next thing I want to show so if I go back to uh the AWS dashboard so here you can see on the configuration section uh you can see email templates okay so this is the previous template I have created so we can also create email templates uh and then we can make use of them in our node project so let's see how to create a template from our node application and then we can make use of that so let's create a new file so in order to create the template so again from our client SES Library so we need to import this client as well as create template command so again the configuration and creating a service object is same so you can see config object with credential synthesian and and we are creating a client SES client using that configuration so here I'm going to create a function called run and to this so I will provide the template name okay so inside this so now you can observe this one like uh so we are creating an a template object using this create template command by providing some options to this so you can also observe that template feature in SCS is based on handlebar template system so that's why you know if if you have any Dynamic data then you need to use the handlebar way of using applications so double curly braces so here you can see the first object the only object inside this is template option so inside it you are you are supposed to provide the name template name so here we are receiving on as a function parameter so the HTML part so you can provide your HTML code as per your requirement and again simply uh the text version and the subject part so this is how you will create a template by providing the template data okay so now uh so we have created the template Command right so from this onwards like again so here you can see using the client so we are calling the sun method by you passing this uh create template object so this will create a template in our SCS dashboard so let me save this foreign has been created so this is our coming from our log and then the response object okay let me refresh this again all right now I can see the template is created let's go back to our code and this time so I'm just going to create a one more file so template mail using version three so here you can see so again from the client package so I'm this time we are using the template email command the configuration and creating the service object is same so let's create this function and so while calling this we are supposed to mention so which template we want to use and then so the mail address that we want to use so here I'm using this okay so the template we have created is uh this one right so let's make use of this foreign template command so we need to create an object using that Constructor so here you can see so if you pass the template uh like this in your template if you pass you know information like this so it will interpret in your mail body like this so here you can see the destination the two recipient mail address Source this these things are common for all the places right so here you can observe so to the template we need to provide the template name whichever we are passing here so from the parameters like we need to pass that this is the template data so this is the dynamic data that you know we want to so here I'm updating this to this name so we we are we need to string by this so that it will be in a Json format and so so this is the name property that you know we are making use of inside the template like if you if I expand this so you can see in the HTML part we are accessing this so this is coming from this uh template data which is the dynamic data all right so now we need to pass this object to our send command from our ICS client so you can see using our client I am calling send method by passing this template command so let me save this so this time uh you know previously you can observe like on the mails like the text this is the test body of my email right so now this time with a new mail the template should be different so let's execute that so you can see I'm I'm executing this file so this is based on the template that we have created okay so email uh with the template has been sent okay now you can see this time we got a new mail so if I open this so you can see the subject is different you can see this is that in the HTML like we are using we are making use of the name right so this is the thing and again this is the paragraph that we are using inside it that's all about the video guys uh so let me open the code base again so totally we have covered uh sending an email using the AWS SDK by importing this uh library and during this approach like we have encountered the migration warning so uh we have resolved that by suppressing this as well as uh we have used another approach using the modular approach right so this is that one so after this one we have learned how to create a template so uh based on our requirement and then sending an email using the template so this is the one I will provide a few uh documentation links that will help you to a better understand it just go through these things you know to better understand why we need a you know modular approach and all I hope you find this video helpful do like and subscribe to the channel thank you for watching and we will meet in another video Until then take care bye
Info
Channel: Web Wizard
Views: 13,416
Rating: undefined out of 5
Keywords:
Id: uv6O22OLFoU
Channel Id: undefined
Length: 24min 25sec (1465 seconds)
Published: Tue Jun 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.