AWS Simple Email Service + Serverless Vercel + Node.js Deploy&Receive Emails For Free
Tags: aws, opensource, node, serverless
Published: 2022-06-12 18:27:33

**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);
```

We don't need Post Routes anymore.

HTML>
```
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div>Works!</div>
</body>
</html>
```

2. Setup AWS Simple Email Notification Service.
https://us-east-1.console.aws.amazon.com/ses/home?region=us-east-1#/homepage

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxbz85aaadc6x9z6bj27.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aaq2ikeqabdn5vx764j8.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1oz4dtcv2jcab8c7t3i.png)


You will need to confirm your email. 

Done. 

3. 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. 

4. 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");
  //
}
```

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

### Conclusion

Thanks For Reading.

### Links

🖇 Follow me on [GitHub](https://github.com/aserputov) 

🖇 Follow me on [Twitter](https://twitter.com/aserputov)

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