Encountering a problem with my Amazon SES code in Next.js. Unsure where the mistake lies, seeking assistance to resolve the error. Feel free to ask if you have any questions.
sendmail.js
This is where I encountered an issue in the sendmail.js file while utilizing Amazon SES for email delivery.
var AWS = require('aws-sdk');
AWS.config.update({ region: process.env.AWS_REGION });
// var mail = '';
function sendMail(Email) {
var result;
// Create sendEmail params
var params = {
Destination: { /* required */
CcAddresses: [
Email,
/* more items */
],
ToAddresses: [
Email,
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: "HTML_FORMAT_BODY"
},
Text: {
Charset: "UTF-8",
Data: "TEXT_FORMAT_BODY"
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Test email'
}
},
Source: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f3e3d3c1f38323e3633713c3032">[email protected]</a>', /* required */
ReplyToAddresses: [
'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="26474445171466414b474f4a0845494b">[email protected]</a>',
/* more items */
],
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({ apiVersion: '2010-12-01' }).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function (data) {
result = 'Success';
}).catch(
function (err) {
result = 'Failed';
});
}
export default sendMail;
dynamicid.js
This is where I implemented my endpoint code in the dynamic id.js file.
import { getDataFromSheets } from '../../../libs/sheets';
import sendmail from '../../../libs/ses/sendmail';
export default function handler(req, res) {
var data;
getDataFromSheets()
.then(sheet => {
data = sheet.length
for (var i = 1; i < data; i++) {
sendmail(sheet[i].Email)
}
})
.catch(err => console.log(err))
}