DEV Community

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

Posted on

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.

Top comments (0)