How to send emails with attachments using the Node.js API for Amazon SES

Sep 20, 2017 using tags aws, nodejs

Sending a regular plain text or HTML email with SES is simple enough with the AWS SDK for Javascript.

import SES from 'aws-sdk/clients/ses';

// ...

const ses = new SES();

return ses.sendEmail({
  Source: '',
  ReplyToAddresses: '',
  Destination: {
    ToAddresses: ['', ''],
  Message: {
    Subject: {
      Data: 'Sample SES message',
    Body: {
      Text: {
        Data: 'Hey folks, this is a test message from SES',

When you need to do anything fancier than that - for example sending an email attachment - the sendEmail function no longer suffices; You need to use the sendRawEmail function instead which takes a bit more work.

With the sendRawEmail function you need to create the raw mime message yourself which can get tedious. This is where the mailcomposer library is useful.

import SES from 'aws-sdk/clients/ses';
import mailcomposer from 'mailcomposer';

// ...

return Promise.resolve().then(() => {
  let sendRawEmailPromise;

  const mail = mailcomposer({
    from: '',
    replyTo: '',
    to: '',
    subject: 'Sample SES message with attachment',
    text: 'Hey folks, this is a test message from SES with an attachment.',
    attachments: [
        path: '/tmp/file.docx'

  return new Promise((resolve, reject) => {, message) => {
      if (err) {
        reject(`Error sending raw email: ${err}`);
      sendRawEmailPromise ={RawMessage: {Data: message}}).promise();


The npm packages related to the above examples are:

$ npm install aws-sdk mailcomposer

Tip: mailcomposer documentation

The documentation for the mailcomposer API is located under the version tag specific to the release. For example, the v4.0.1 documentation is available at