Description
Checkboxes for prior research
- I've gone through Developer Guide and API reference
- I've checked AWS Forums and StackOverflow.
- I've searched for previous similar issues and didn't find any solution.
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.