Skip to content

Sending an attachment using SESv2Client raises a SerializationException #7078

Open
@aivot-on

Description

@aivot-on

Checkboxes for prior research

Describe the bug

Sending an email using sesv2 SendEmail doesn't encode the attachment when UInt8Array / Buffer is passed as RawContent.

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/client-sesv2@latest

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

nodejs:22.v39 arn:aws:lamdba:eu-north-1

Reproduction Steps

import { SendEmail, SESv2Client } from '@aws-sdk/client-sesv2';
import { readFileSync } from 'fs';
const attachmentData = readFileSync('example.png'); // Buffer/UInt8Array
const ses = new SESv2Client();
await ses.send(
  new SendEmail({
    FromEmailAddress: 'example@example.com',
    Destination: { ToAddresses: ['example@example.com'] },
    Content: {
      Simple: {
        Subject: {
          Charset: 'utf-8',
          Data: 'Example',
        },
        Body: {
          Charset: 'utf-8',
          Html: '<p>Example</p><img src="cid:example" alt="Image"/>',
        },
        Attachments: [
          {
            RawContent: attachmentData,
            ContentDisposition: 'INLINE',
            ContentTransferEncoding: 'BASE64',
            FileName: 'example.png',
            ContentId: 'example',
          },
        ],
      },
    },
  }),
);

Observed Behavior

SerializationException: Start of structure or map found where not expected.
at throwDefaultError (/var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:867:20)
at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:876:5
at de_CommandError (/var/runtime/node_modules/@aws-sdk/client-sesv2/dist-cjs/index.js:3594:14)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20
at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/core/dist-cjs/index.js:167:18
at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38
at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22
at async send (file:///var/task/index.mjs:92:22)
at async handle (file:///var/task/index.mjs:168:9) {
'$fault': 'client',
'$metadata': {
httpStatusCode: 400,
requestId: '.....',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
}

Expected Behavior

No SerializationException

Possible Solution

Current workaround within lambda is to encode the UInt8Array as base64 on SerializationException.

try {
  const binary = new UInt8Array(...);
  await sendMail(binary);
} catch (err) {
  if (err.name === 'SerializationException') {
    await sendMail(Buffer.from(binary).encode('base64'));
  }
}

Additional Information/Context

The ses documentation mention that using sdk the uint8array shouldn't be encoded, but it seems that within aws lambda the sdk expects the content be already encoded. Not sure if this is docs issue or a bug.

I would assume that the same javascript would run the same regardless whether it's ran on aws lambda or on some other environment.

https://repost.aws/articles/ARFNjNl2gISmOkPzXb8A-nDQ/how-can-i-use-ses-v2-api-send-email-with-attachment

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.investigatingIssue is being investigated and/or work is in progress to resolve the issue.p2This is a standard priority issueresponse-requestedWaiting on additional info and feedback. Will move to \"closing-soon\" in 7 days.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions