I want to retrieve an ejs file data and then sending the ejs file content as data in email as AWS SES. For this to get the path of the file I’m using __dirname. The code works on the localhost but when its hosted on aws it does not recognizes __dirname.
export const sendVerificationEmail = async (email: string, token: string, req: Request): Promise<void> => {
// Read the template file for the verification email
const templatePath = path.join(__dirname, '../../../views/verifyEmail.ejs');
// Read the template file for the verification email
const template = fs.readFileSync(templatePath, 'utf-8');
// Render the email content with the verification link and other variables
const emailContent = ejs.render(template, {
verificationUrl: process.env.verificationLink,
logo: process.env.LOGO,
playStore: process.env.GOOGLE_PLAYSTORE,
appleStore: process.env.APPLE_STORE,
});
// Prepare the email parameters
const params = {
Destination: {
ToAddresses: [email],
},
Message: {
Body: {
Html: {
Data: emailContent,
},
},
Subject: {
Charset: 'UTF-8',
Data: 'Email Verification',
},
},
Source: '[email protected]', // Replace with your SES verified email
};
try {
// Send the email using AWS SES
await ses.sendEmail(params).promise();
} catch (error: any) {
logger.error('AWS SES Error', {
message: 'Error sending email',
errorCode: 500,
errorString: error.message,
type: 'sendVerificationEmailError',
});
// Send a 500 status code with an error message.
return res.status(500).send({ message: 'Internal server error: '+ error.message });
}
};
ERROR:
“message”: “Internal server error: __dirname is not defined”