DEV Community

Cover image for A Node Email Service using AWS lambda
Dhilip kumar
Dhilip kumar

Posted on

A Node Email Service using AWS lambda

Hello there,

It is always so rewarding when you achieve something after long research. And this is one such instance as I had to at least read 40+ blogs to finally set up a fully running Mailing service. So just thought of collating everything together in a blog to help lambda newbies like me.

Prerequisites:

  • Javascript
  • A Gmail account

The Problem:

Having Contact Me like the one below is an integral part of most of the personal and small business websites that are built.

I was building one such website, as the entire website is static, I honestly do not want to set up a server just to expose a single endpoint.

The solution:

I know that cloud functions are something that solves my problem of having an endpoint without actually setting up the server. I chose AWS Lambda as it was much popular. But, the resources and blogs were not enough to give me a step by step guideline.

What are we building?

We are going to build a node emailer service that accepts a message in our POST request body and triggers an email to the predefined set of recipients from your Gmail account.

Table Of Contents

1.AWS Account Setup:

Create an AWS account here. Your account setup will be complete with you entering your Credit card details and verifying your email. Charges are usage-based.

2.Setting up Lambda:

  • Naviagte to AWS Console
  • Choose Lambda under the Find Services input field.
  • You should now be on the lambda functions dashboard which shows the list of your available lambda functions.
  • Click on the Create Function button
  • In the next screen, fill in your Function name - emailer and choose Nodejs runtime as we are implementing this using node.
    Alt Text

  • On Clicking the Create function button you should see Successfully created the function emailer message on the next screen.

  • On scrolling down the page, you will see a sample nodeJS code with index.js

  • Create a new test with any name of your choice and click on the Test button, you should be getting the response in the Execution Result tab.

3.Uploading the emailer code to your lambda:

The Aws lambda IDE for nodeJS does not allow us to install our npm packages on the go. Due to this, we have to get this set up locally in our machine and then upload the code to lambda by zipping it.

  • Download the Zip. It contains the code to be uploaded to your lambda function.
  • If you want to create the zip, the content is present inside this repo where there are a nodemailer dependency and some code to send an email. Make sure to npm install and create a zip from the root directory including your node_modules folder.
  • Once you got the Zip, upload it to the AWS lambda using Actions -> Upload a .zip file option.

Alt Text

  • If you open index.js you should be able to see the code where we have given our Email credentials and sending an email.
  • Headers are set to handle CORS errors if you try hitting your lambda from another origin.

4. Google Oauth and GCP Setup:

  • You need to set the following auth keys in order to confirm that you are the owner of your email account.
{
    clientId: '<YOUR_CLIENT_ID>',
    clientSecret: '<YOUR_CLIENT_SECRET>',
    refreshToken: '<YOUR_REFRESH_TOKEN>',
    accessToken: '<YOUR_ACCESS_TOKEN>'
}

Enter fullscreen mode Exit fullscreen mode
  • In order to do that this we need to get our Oauth credentials from our GCP project and then use that in Google OAuth playground to generate these keys against your email.

I know it could be a lot of jargon. But trust me it is simple.

Setting up GCP:

  • So login to Google Cloud and create a new project.
  • Click on Select Project and then Create new project button.
  • Name it mailer and click on create.
  • In your mailer project goto APIs & Services -> Credentials -> Create Credentials -> OAuth Client ID -> Configure Content Screen -> External -> Create
  • Again goto Create Credentials -> OAuth Client ID -> Web Application -> Enter Application Name -> Choose https://developers.google.com/oauthplayground as Authorised redirect URIs and Save it.
  • Now you should be getting a popup with your clientID and clientSecret copy both.

Setting up OAUTH:

  • Navigate to Google OAuth Playground
  • Click on Setting icon on the top right corner -> Enable Use your own OAuth credentials > Enter OAuth clientID & Oatuh clientSecret that you got from the above steps -> Close.
  • In Select & Authorize APIs Field, Type https://mail.google.com -> Authorize APIs -> Login with the account that you want to send email from.
  • Click on Exchange authorization code for tokens -> Copy Refresh Token and Access Token.

5. Update the keys in your Code:

Now we got all the keys needed.
Now update your clientId, clientSecret, refreshToken, and accessToken and your Full email ID in the AWS Lambda code.

  • Click Deploy -> Test -> Configure your test to include message parameter.

  • You should get an email with your message on clicking Test.

6. Creating AWS API Gateway:

  • Create an API to expose this lambda function as a service.
  • Click on Services -> API gateway service from the search bar -> Create API -> REST API -> Build -> API name -> Create.
  • You should be on this screen now.

Alt Text

  • We need two methods to be created. 1.POST and 2.OPTIONS to handle CORS.

Creating POST:

  • Actions -> Create Method -> POST -> TICK -> Integration type -> Lambda -> Lambda Function -> emailer -> Save -> OK.
  • We need to allow few Headers so that they could be read by the client.
  • Method Response -> Expand the Accordion next to 200.

Alt Text

Add the following headers


    Access-Control-Allow-Headers
    Access-Control-Allow-Methods
    Access-Control-Allow-Origin

Enter fullscreen mode Exit fullscreen mode
  • Go to Integration Response -> Expand Accordion -> Header Mappings -> Make the following

Access-Control-Allow-Origin : '<YOUR_DOMAIN>'


Enter fullscreen mode Exit fullscreen mode
  • If you have multiple headers being passed from your API, in order to consume them you have to enable it here.
  • You can now do a test from the TEST option -> pass the following in the body

{
    "message": "HELLO"
}

Enter fullscreen mode Exit fullscreen mode
  • Click on Test -> you should get an Email with "HELLO" in the Message
  • Actions -> Deploy API -> Deployment Stage (New Stage) -> Dev as Stage Name -> Deploy.
  • Your POST API is Now deployed.
  • Copy the INVOKE URL
  • POST call this INVOKE URL with message param in body to send the email.

Similarly, create the OPTIONS method, and update the headers. You should get an 'OK' response to testing the Same.
It is mandatory otherwise your Cross-site requests will fail.

Now do this


fetch(INVOKE URL, {
    method: 'POST',
    body: JSON.stringify({ message: 'hi'})
}).
then(res => res.json()).
then(res => console.log(res)); // {"message":"Email processed succesfully!"}

Enter fullscreen mode Exit fullscreen mode

You have Done it!

Don't forget to follow me 😄

My Website, blogs and Twitter

Thats all Folks!!!

Top comments (2)

Collapse
 
insanenaman profile image
Naman Gupta

That's great. For simple solution you can use something like this staticforms.xyz/. I am not sure but you can just hit their endpoint with POST request and VOILA .. they will forward the response to your email.

Collapse
 
dhilipkmr profile image
Dhilip kumar

This one's good! Thanks for sharing 🤓