(intermediate value).sendEmail(...).promise is not a function

Viewed 594

I'm using aws-sdk in my service to send emails. I'm getting below exception to a code which was working fine before.

const aws = require('aws-sdk');

var params = {
  Destination: { 
    ToAddresses: [
      'checkMail@gmail.com'
    ]
  },
  Message: { 
    Body: { 
      Html: {
       Charset: "UTF-8",
       Data: "HTML_FORMAT_BODY"
      },
      Text: {
       Charset: "UTF-8",
       Data: "this is sample"
      }
     },
     Subject: {
      Charset: 'UTF-8',
      Data: 'Test email'
     }
    },
  Source: 'AWS Services<awsEmails@awsService.com>'
  ReplyToAddresses: [
     'AwsServices<noreply@awsServices.com>'
  ],
};

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

// Handle promise's fulfilled/rejected states
emailPromise.then(
  function(data) {
      //my logic on success goes here
  }).catch(
    function(err) {
      //my logic on error goes here      
  });

I have tried using different API calls for email from AWS but all returns the same error.

1 Answers
  1. Avoid require ALL aws-sdk if it's just to use a single service. For using SES, you can yarn add @aws-sdk/client-ses and then use it const { SESClient, SendEmailCommand } = require("@aws-sdk/client-ses");

I show you here a full exemple of sending email with SES in a nodeJS lambda function:

const {
  SESClient,
  SendEmailCommand,
} = require("@aws-sdk/client-ses");
const REGION = "eu-west-3"; // Use you AWS region
const ses = new SESClient({ region: REGION });

exports.handler = async function (event) {
  const path = event.path;
  
  if (path === "/send-email") {
    const peopleAmount = 12;
    const params = {
      Source: "John Wick <john.wick@killer.com>",
      Destination: {
        ToAddresses: ["adresse1@test.com"],
      },
      Message: {
        Body: {
          Html: {
            Data: `<span>This email is about <b>${peopleAmount}</b> people.</span>`,
          },
        },
        Subject: {
          Data: "Email Title",
        },
      },
    };
    
    try {
      const data = await ses.send(new SendEmailCommand(params));

      return {
        statusCode: 200,
        body: peopleAmount,
      };
    } catch (e) {
      console.error(e, e.stack);
      return {
        statusCode: 400,
        body: "Sending failed",
      };
    }
  }
}

I hope it helps, here is documentation to use SES email template.

Related