Search code examples
amazon-web-servicesnodemaileramazon-ses

Send emails with Nodemailer and AWS SES with imports


Although the official Nodemailer documentation explains how to use Nodemailer with AWS SES, the sample code uses require statements.

How to do the same with import statements?


Solution

  • Nodemailer can be used with AWS SES with:

    import nodemailer from 'nodemailer'
    import * as aws from '@aws-sdk/client-ses'
    
    const ses = new aws.SES({
      apiVersion: "2010-12-01",
      region: process.env.AWS_REGION,
      credentials: {
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
        accessKeyId: process.env.AWS_ACCESS_KEY_ID
      }
    });
    
    const transport = nodemailer.createTransport({
      SES: { ses, aws }
    });
    
    let info = await transporter.sendMail({
      from: '"Fred Foo 👻" <[email protected]>',
      to: "[email protected], [email protected]",
      subject: "Hello ✔",
      text: "Hello world?",
      html: "<p>Hello world?</p>"
    });
    

    PS: The trick about the AWS import was originally posted as a GH comment.