skip to Main Content

What is the correct and up to date method of sending AWS SES? I am having a hard times finding any info and when I construct config file it seems that this is no longer valid

const client = new aws.SES({
  region: "eu-west-1",
  apiVersion: "2010-12-01",
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});

secretAccessKey and accessKeyId are now deprecated. I have found a new package @aws-sdk/client-ses v3 but the params object accepts only two keys which can be of string or undefined and I don’t know where should the email data go. I am trying to read the docs, but it’s a pain… https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-ses/classes/sesclient.html
this is my code so far

import { NextApiHandler } from "next";
import {
  SESClient,
  CloneReceiptRuleSetCommand,
  CloneReceiptRuleSetCommandInput,
} from "@aws-sdk/client-ses";

const contactFormHandler: NextApiHandler = async (req, res) => {
  if (req.method !== "POST") {
    return res.status(405).setHeader("Allow", "POST").json({});
  }
  const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
  const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
  const recipientEmail = process.env.AWS_SES_TO_EMAIL;
  const senderEmail = process.env.AWS_SES_FROM_EMAIL;
  const { email, message } = req.body;

  if (!accessKeyId || !secretAccessKey) {
    return res.status(500).json({ error: "Invalid credentials" });
  }
  if (!email || !message) {
    return res.status(400).json({ error: "No data to send" });
  }
  if (!senderEmail || !recipientEmail) {
    return res.status(500).json({ error: "Sender or recipient not provided" });
  }

  const client = new SESClient({
    region: "eu-west-1",
    apiVersion: "2010-12-01",
    credentials: {
      accessKeyId,
      secretAccessKey,
    },
  });

  const params: CloneReceiptRuleSetCommandInput = {
    OriginalRuleSetName: undefined,
    RuleSetName: undefined,
  };
  const command = new CloneReceiptRuleSetCommand(params);
  try {
    const data = await client.send(command);
    return res.status(200).json({ message: "all good" });
  } catch (error) {
    return res.status(500).json({ error });
  }
};

export default contactFormHandler;

can someone share experience?
thanks

2

Answers


  1. You are referring only to the reference docs. To work with AWS SDK for JavaScript v3, refer to the Developer Guide:

    AWS SDK for JavaScript Developer Guide for SDK Version 3

    This guide contains the information you need to know to successfully work with this SDK. For example, to work and set your credentials, see:

    Setting Credentials

    Finally — you can find many SES Code examples that use the AWS SDK for JavaScript in this Github repo.

    https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/ses

    The Send Email example is included here.

    Login or Signup to reply.
  2. You can do following:

    import aws from "aws-sdk";
    
    aws.config.update({
       region: "...",
       credentials: {
          accessKeyId: "...",
          secretAccessKey: "..."
       }
    })
    

    In the next line you can now use your SES

    const client = new aws.SES();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search