DEV Community

Cover image for AWS Simple Email Service + Serverless Vercel + Node.js Deploy&Receive Emails For Free
A Serputov
A Serputov

Posted on

3 2

AWS Simple Email Service + Serverless Vercel + Node.js Deploy&Receive Emails For Free

This is a quick guide: On How To Deploy(Create) Node.js Server With AWS SES on Vercel(Heroku, Netlify, e.t.c)

Our Code will Send A Confirmation Email On Form Submission.

  1. Create A Server
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 8080;

app.get("/home", function (req, res) {
  res.sendFile(path.join(__dirname, "/index.html"));
});

app.listen(port);

console.log("Server started at http://localhost:" + port);
Enter fullscreen mode Exit fullscreen mode

We don't need Post Routes anymore.

HTML>

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div>Works!</div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
  1. Setup AWS Simple Email Notification Service. https://us-east-1.console.aws.amazon.com/ses/home?region=us-east-1#/homepage

Image description

Image description

Image description

You will need to confirm your email.

Done.

  1. In HTML we need to add <form action="/api/hello" method="POST" style=" padding: 1%;margin-left:25%;width: 50%;">

/api/hello is the endpoint for Serverless in Vercel.

  1. Create Your Logic Inside hello.js file in api folder.
var aws = require("aws-sdk");

export default function hello(req, res) {
  const formData = req.body;
  console.log(req.body.name);

  aws.config.update({
    credentials: {
      accessKeyId: process.env.AWS_ACCESS_KEY_ID_MYAPP,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY_MYAPP,
    },
    region: process.env.AWS_REGION_MYAPP,
  });

  // AWS.config.update({region: });

  var params = {
    Destination: {
      /* required */
      CcAddresses: [
        "ser****@gmail.com",
        /* more items */
      ],
      ToAddresses: [
        "serp****@gmail.com",
        "additionalemail@gmail.com",
        /* more items */
      ],
    },
    Message: {
      /* required */
      Body: {
        /* required */
        Text: {
          Charset: "UTF-8",
          Data: `${JSON.stringify(formData)}`,
        },
      },
      Subject: {
        Charset: "UTF-8",
        Data: "Test email",
      },
    },
    Source: "serp****@gmail.com" /* required */,
    ReplyToAddresses: [
      "serpu****@gmail.com",
      /* more items */
    ],
  };

  // Create the promise and SES service object
  var sendPromise = new aws.SES({ apiVersion: "2010-12-01" })
    .sendEmail(params)
    .promise();

  // Handle promise's fulfilled/rejected states
  sendPromise
    .then(function (data) {
      console.log(data.MessageId);
      res.status(200).send(`Hello Thank you!!`);
    })
    .catch(function (err) {
      console.error(err, err.stack);
    });
  //   res.redirect("/home");
  //
}
Enter fullscreen mode Exit fullscreen mode

Don't FORGET to add env variables inside your Vercel Website.

Conclusion

Thanks For Reading.

Links

🖇 Follow me on GitHub

🖇 Follow me on Twitter

_p.s This post was made out of my curiosity.

Image of Datadog

Learn how to monitor AWS container environments at scale

In this eBook, Datadog and AWS share insights into the changing state of containers in the cloud and explore why orchestration technologies are an essential part of managing ever-changing containerized workloads.

Download the eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay