I am trying to send an email with the aws-sdk client-ses.
my SES_AWS* credentials are all valid.
Is this right??
SES_AWS_EMAIL_OWNER = ses-smtp-user.20250117-103624
also tried:
SES_AWS_EMAIL_OWNER = [email protected]
Error
Here is the message I am getting:
The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details
Code
const { SESClient, SendEmailCommand } = require('@aws-sdk/client-ses');
const SES_AWS_REGION = process.env.SES_AWS_REGION;
const SES_AWS_ACCESS_KEY_ID = process.env.SES_AWS_ACCESS_KEY_ID;
const SES_AWS_SECRET_ACCESS_KEY = process.env.SES_AWS_SECRET_ACCESS_KEY;
const SES_AWS_EMAIL_OWNER = process.env.SES_AWS_EMAIL_OWNER;
const what = 'emailer';
const emailer_debugThis = true;
const logger = emailer_debugThis ? console : null;
// Initialize the SES client
const sesClient = new SESClient({
region: SES_AWS_REGION,
credentials: {
accessKeyId: SES_AWS_ACCESS_KEY_ID,
secretAccessKey: SES_AWS_SECRET_ACCESS_KEY,
},
signingRegion: SES_AWS_REGION,
signingName: 'ses',
logger: logger,
});
const sendEmail = async (
caller,
toAddresses,
subject,
body,
simulate,
caller_debugThis
) => {
const msgPrefix = caller + '.' + what + '.sendEmail';
const debugThis = false || emailer_debugThis || caller_debugThis;
// make sure toAddresses is always an array
toAddresses = Array.isArray(toAddresses) ? toAddresses : [toAddresses];
if (debugThis) {
console.log(msgPrefix, 'simulate', simulate);
console.log(msgPrefix, 'from', JSON.stringify(SES_AWS_EMAIL_OWNER));
console.log(msgPrefix, 'toAddresses', JSON.stringify(toAddresses));
console.log(msgPrefix, 'subject', JSON.stringify(subject));
const len = 30;
const logBody = body.length > len ? body.slice(0, len) + '...' : body;
console.log(msgPrefix, 'body', JSON.stringify(logBody));
}
const params = {
Source: SES_AWS_EMAIL_OWNER, // Must be a verified sender email in SES
Destination: {
ToAddresses: toAddresses, // array of email addresses
},
Message: {
Subject: {
Charset: 'UTF-8',
Data: subject,
},
Body: {
Text: {
Charset: 'UTF-8',
Data: body,
},
},
},
};
if (debugThis) {
console.log(msgPrefix, 'params', JSON.stringify(params));
}
let ret = null;
try {
if (!simulate) {
ret = await sesClient.send(new SendEmailCommand(params));
if (debugThis) {
console.log('Email sent successfully:');
}
} else {
if (debugThis) {
console.log('');
console.log(msgPrefix, 'Email sent successfully: SIMULATED');
console.log('');
}
ret = {
simulated: 'true',
};
}
} catch (error) {
console.error(msgPrefix, error.message); // always log errors
ret = {
error: error.message,
};
}
if (debugThis) {
console.log(msgPrefix, 'ret', JSON.stringify(ret));
}
return ret;
};
module.exports = {
sendEmail,
};