So we're going to use Node Mailer as our third party package to manage email sending in Node.
Additionally, we're going to use Gmail as our Smtp provider because it is a free option that will allow
us to send up to 5000 emails a day on a given account, which will work fine for our purposes.
I'm also going to show you how to easily configure Node Mailer to swap in your own custom domain and
your own custom Smtp server in case you want to pay for a third party service that will allow you to
send more emails a day.
So to get started, let's go ahead and stop our containers and we'll go ahead and run NPM install node
mailer and additionally make sure we install the types for node mailer as well, and then we can go
ahead and start our containers back up.
And I'll go ahead and declare a new private read only variable here called transporter.
We're just going to be equal to node mailer dot and we can go ahead and import node mailer up top.
So go ahead and import node mailer from Node Mailer here and then we can call dot, create transport
on it and additionally make sure we import Star as node mailer from Node Mailer here to make sure this
import works properly.
So in Create Transport, this is where we provide the options object, where you can configure your
own Smtp server which will be used to send emails.
So feel free to include your own credentials or follow along with me where we set up a free Gmail account
and create our own Smtp provider.
To do that, we're going to go ahead and specify a service property here where we say this service is
Gmail and then we provide an auth object here.
I'm going to set the type here to OAuth two and then we're going to make use of the config service to
get access to some credentials here.
So I'll have a new private read only config service injected here from nestjs config.
And the first environment variable we're going to extract is the user here and we're going to call this
dot config service dot get TP user.
Next we'll have a client ID property which leaves the config service for again.
This time we will pull off Google OAuth client ID here because this will be the Gmail OAuth client that
we set up in order to authenticate with Node Mailer.
And then we will have a client secret as well that we supply to this transport here.
So we'll call Google OAuth client secret out of the config service now.
And the last property we're going to be needing is a refresh token.
So let's go ahead and supply a refresh token here and we will also pull this off of the config service
and I'll call this refresh token, which we will also generate.
So now that we have our transport at least set up in the service, let's go ahead and call await this
dot transporter dot, send mail where we can supply the mail options we know from is going to be this
dot config service dot get Smtp user which which will be the email for the Gmail account we're going
to set up as our Smtp account and then we can supply the two as the email property passed in from the
notify email.
DTO Next we'll have a subject here set to sleeper notification and then for the text here we will just
have a test text for now to see that things are working.
And then we will modify our DTO to take in custom texts.
So now if we send off a new reservation request here, we can go back to the terminal.
We can see in the notification service we're throwing an error because our authentication has failed.
Obviously we have not provided any of these environment variables to our node mailer transport.
So let's go ahead and set up everything on the Google side so that we can have this Smtp user supplied
and send mail using Gmail.
So in order to actually use Gmail as our email Smtp provider, we'll of course need a Gmail account
or a Google account.
So make sure you have a Google account you want to use or as I'll do here, by creating a new account
and I will use the username here Sleeper Nest app to gmail.com and feel free to use whatever password
you'd like.
Okay, so after creating our gmail account, we can see we are on the default Gmail dashboard here and
now we need to go ahead and set up our Google console account.
Okay.
So go over to Google Cloud and you can use an existing Google Cloud account if you have one.
Or we can go ahead and set one up with our newly created Gmail account.
I'll go ahead and accept the terms and create a new account.
Next, we're going to go ahead and click select a project and create a new project here that will go
ahead and call Sleeper.
Next, we can click on our notifications tab and select this newly created project.
So we're going to go ahead and click on this APIs and Services section over here and then click on.
To the OAuth consent screen.
Let's go ahead and select external and then create.
And now we need to fill out some information about our OAuth consent, which is how we're going to obtain
the necessary app password for this application.
I'll go ahead and call the app name Sleeper.
And for our support email, I will select our default email.
And for this developer contact information, I'll use the Sleeper Nest app two that we just set it up
at gmail.com and then we can go ahead and click Save and continue.
We don't need to worry about scopes, so we'll go down and click Save and continue again.
And for a test user, I'll go ahead and add our existing email as a test user.
So this will be Sleeper nest app two at gmail.com.
And finally scroll down to the bottom where we can go back to the dashboard now.
Now we need to actually create our OAuth credentials.
So let's go to the credentials tab now and click Create Credentials and then click create.
OAuth client ID Next for our application type.
We'll go ahead and select web application and then I will add the name here.
We can call it whatever we want.
I'll just call this sleeper.
And finally, under the authorized redirect Uris section, click add Uri and then paste in this URL
here, which is the OAuth playground for Developers.google.com, which is what we're going to use to
obtain a refresh token with this OAuth application we're creating.
After we've done this, go ahead and click Create.
So after creating, we can see we now have credentials for this application, so let's copy them now.
Well firstly, copy the client ID and head to the dot m file for our notification service where we'll
add the Google OAuth client id M variable and paste it in here.
And secondly, do the same thing for the client secret.
We'll copy this and paste it in as Google OAuth client secret.
We can now close this and head over to the Google OAuth two playground where we can obtain a refresh
token.
Okay, so now that we have our OAuth client configured, we're going to head to Developers.google.com
slash OAuth playground.
And once we're here, we're going to go and hit the settings cog up to the side here.
And then we click use your own OAuth credentials.
Next, you can see we have a section to fill in our client ID and client secret.
So you can get these from our M file, which we just added.
So go ahead and get the client ID out of here, paste it in, and then let's do the same for the client
secret Now.
So after we have our OAuth client configured on the left hand side here where we can authorize APIs,
scroll down until we find the Gmail API, V1, click onto here and then click Mail.google.com.
Lastly, click Authorize APIs.
This is going to allow us to connect our auth client credentials.
So go ahead and sign in with the account that you created earlier.
After we sign in with our current Gmail account, we'll go ahead and click Continue and then go ahead
and click continue.
When it asks if we want to ask Sleeper to be able to read, compose and send emails from our Gmail.
So we'll go ahead and click continue.
And now we can see exchange authorization code for tokens.
Go ahead and click this button.
And now importantly, we can get this refresh token, which is the value we need to plug into our application.
So go ahead and copy this, refresh token, we'll copy this refresh token and paste it in here under
Google, OAuth refresh token, paste it in.
And lastly, if we remember back in the notification service, we also had to define the Smtp user,
which in our case is going to be the Gmail email we're sending from.
So we'll use Smtp user and set this equal to the Gmail account you created.
And in my case, I'll use my other Smtp account Sleeper Nest app at gmail.com.
Now that we've added these additional environment variables to the dot m file, we need to go back to
the notifications module and update our validation schema to include the rest of these Google variables.
So let's go ahead and add these.
So I've gone ahead and copied over the new environment variables, the Google OAuth ones, which are
all required strings as well as the Smtp user, which is also required string.
Feel free to pause now and fill out the rest of the schema before we move forward.
Okay, so now that we have everything we need in our notification service to configure our node mailer
transport, let's go ahead and test things out.
Back in Postman, we want to make sure we create a user that leads to an email we can actually test
out.
So make sure we actually have created a user.
So let's go ahead and create an user by hitting localhost 3001 slash users and this will be a post request.
I'll go ahead and change the email to where I want to be notified when I create a new reservation.
In this case, I'll use the Gmail account we set up together.
But you can feel free to change this out with your own email address and I will keep the password the
same.
So after we create this new user, let's go ahead and actually log in with it first.
So we have our JWT set.
I'll send a request to all slash log in and keep the payload the same here.
You can see we succeeded and we should have our Json web token cookie in here.
And so now let's go back to localhost 3000 slash reservations.
What?
We're going to create a new reservation.
After we charge this card with Stripe, we know we're going to be emitting event to the notification
service to notify the user associated with this request on that JWT, which we know is the newly created
user.
So let's go ahead and send off a new reservation.
We can see the reservation was created.
So now that the reservation has been created back in the Gmail account that I associated with this user,
we can see we have a new email titled Sleeper Notification from the Sleeper Nest app.
And importantly, we have the test text that we included here.
So lastly, let's extend our notify email DTO to take in text so that this can be customized.
I'll go ahead and add a new is string field called text of type string.
Lastly, let's open up the payment service where we know we're actually calling notification service
and we'll now include a new text property.
So inside of this text property, we'll go ahead and open up a template literal here and say payment
of and then open up a variable here.
We'll say amount times 100 has completed successfully and we'll go ahead include a dollar sign before
this.
Just for some formatting, you can actually go ahead and get rid of the multiplying by 100 here because
we know the value we're supplying is already going to be in dollar amounts.
So now that we've completed this, let's go ahead and back to Postman and send off another reservation
so we can test out this text.
Okay.
So now back in this Gmail, I can see we have a new email from Sleeper and now we can see our text which
says Your payment of $5 has completed successfully.
So now we're able to notify users from anywhere in our system by calling our notifications microservice
and having Gmail send emails for us.