Skip to content

Unable to add attachment in AWS SES using SendRawEmail

0

I am using AWS SES service and I have followed AWS documentation https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-ses/classes/sendrawemailcommand.html for SES SendRawMail which provides a feature to add attachments with the email.

My code Implementation :

const { SESClient, SendRawEmailCommand } = require("@aws-sdk/client-ses");

const client = new SESClient({
    region : "us-east-1",
    credentials: {
      accessKeyId : "**************",
      secretAccessKey : "**************"
    }
});


var json = {
  "colors": [{
          "color": "black",
          "category": "hue",
          "type": "primary",
          "code": {
              "rgba": [255, 255, 255, 1],
              "hex": "#000"
          }
      }
  ]
};

var file = new Buffer(JSON.stringify(json)).toString("base64");

const payload = `From: *****@gmail.com
To: a**********@gmail.com
Subject: AWS SES Attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="NextPart"
--NextPart
Content-Type: text/html; charset=us-ascii
This is the <b>body</b> of the email.
--NextPart
Content-Type: multipart/form-data;
Content-Disposition: attachment; filename="sample.txt"
Content-Transfer-Encoding: base64
${file}
--NextPart--`

const input = {
    "Destinations": ["********@gmail.com"],
    "FromArn": "",
    "RawMessage": {
      "Data": payload
    },
    "ReturnPathArn": "",
    "Source": "*********@gmail.com",
    "SourceArn": ""
  };
  const command = new SendRawEmailCommand(input);
  const response = client.send(command).then(res => res);

I am getting this error while working on my javascript application *

/node_modules/@aws-sdk/util-buffer-from/dist-cjs/index.js:6 const fromArrayBuffer = (input, offset = 0, length = input.byteLength - offset) => { ^

TypeError: Cannot read properties of undefined (reading 'byteLength') at fromArrayBuffer (/home/cedcoss/anuj-work/research/ses-attached/node_modules/@aws-sdk/util-buffer-from/dist-cjs/index.js:6:60) at Object.toBase64 [as base64Encoder] (/home/cedcoss/anuj-work/research/ses-attached/node_modules/@aws-sdk/util-base64/dist-cjs/toBase64.js:5:68) at se_RawMessage (/home/cedcoss/anuj-work/research/ses-attached/node_modules/@aws-sdk/client-ses/dist-cjs/protocols/Aws_query.js:4267:35) at se_SendRawEmailRequest (/home/cedcoss/anuj-work/research/ses-attached/node_modules/@aws-sdk/client-ses/dist-cjs/protocols/Aws_query.js:4661:31) at se_SendRawEmailCommand (/home/cedcoss/anuj-work/research/ses-attached/node_modules/@aws-sdk/client-ses/dist-cjs/protocols/Aws_query.js:546:12) at serialize (/home/cedcoss/anuj-work/research/ses-attached/node_modules/@aws-sdk/client-ses/dist-cjs/commands/SendRawEmailCommand.js:40:55) at /home/cedcoss/anuj-work/research/ses-attached/node_modules/@aws-sdk/middleware-serde/dist-cjs/serializerMiddleware.js:12:27 at /home/cedcoss/anuj-work/research/ses-attached/node_modules/@aws-sdk/middleware-endpoint/dist-cjs/endpointMiddleware.js:20:16 at async /home/cedcoss/anuj-work/research/ses-attached/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:7:26

Node.js v20.3.0 *

  • Hi Anuj.

    It will be very helpful if you can post your code as well to understand how you are implementing the Javascript SDK. Please ensure you remove any identifiable information such as IDs, ARNs, etc.

    Thanks.

  • I've just edited my question, added my code implementation as well.

asked 3 years ago793 views
1 Answer
0

Anuj, have you tried adding the Content-Length header with the size (in bytes) for the email and/or the attachment?

AWS
EXPERT
answered 3 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.