This commit is contained in:
AllenTJ7 2025-06-24 18:13:44 +05:30
parent 1c0951b47b
commit 5c886ebe42

View File

@ -21,7 +21,7 @@ interface EmailRequest {
interface Attachment {
filename: string;
content: string | Buffer; // Base64 encoded string or Buffer
content: string | Buffer;
contentType?: string;
}
@ -32,7 +32,7 @@ const stripHtml = (html: string): string => {
async function sendSimpleEmail(data: EmailRequest, recipients: string[]) {
const ses = new SESClient({
region: 'ap-south-1',
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '',
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || ''
@ -58,7 +58,7 @@ async function sendSimpleEmail(data: EmailRequest, recipients: string[]) {
async function sendEmailWithAttachments(data: EmailRequest, recipients: string[]) {
const ses = new SESClient({
region: 'ap-south-1',
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '',
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || ''
@ -72,26 +72,21 @@ async function sendEmailWithAttachments(data: EmailRequest, recipients: string[]
rawMessage += `MIME-Version: 1.0\n`;
rawMessage += `Content-Type: multipart/mixed; boundary="${boundary}"\n\n`;
// Add email body (multipart/alternative)
rawMessage += `--${boundary}\n`;
rawMessage += `Content-Type: multipart/alternative; boundary="alt_${boundary}"\n\n`;
// Text part
if (data.text) {
rawMessage += `--alt_${boundary}\n`;
rawMessage += `Content-Type: text/plain; charset=UTF-8\n\n`;
rawMessage += `${data.text}\n\n`;
}
// HTML part
rawMessage += `--alt_${boundary}\n`;
rawMessage += `Content-Type: text/html; charset=UTF-8\n\n`;
rawMessage += `${data.html}\n\n`;
// Close alternative part
rawMessage += `--alt_${boundary}--\n\n`;
// Add attachments
for (const attachment of data.attachments || []) {
const contentType = attachment.contentType ||
mime.lookup(attachment.filename) ||
@ -109,7 +104,6 @@ async function sendEmailWithAttachments(data: EmailRequest, recipients: string[]
rawMessage += contentBuffer.toString('base64') + '\n\n';
}
// Close message
rawMessage += `--${boundary}--`;
const command = new SendRawEmailCommand({
@ -140,7 +134,6 @@ export async function sendEmailWithAttachmentUtil(
try {
logger.info(`Sending email with attachment to: ${toAddress}`);
// Initialize data with basic fields
const data: EmailRequest = {
to: toAddress,
html: message,
@ -151,13 +144,11 @@ export async function sendEmailWithAttachmentUtil(
attachments: []
};
// Handle file URL if provided
if (fileUrl && fileName) {
logger.info(`Downloading attachment from URL: ${fileUrl}`);
try {
const fileContent = await downloadFileFromUrl(fileUrl);
// Add the downloaded file as an attachment
data.attachments!.push({
filename: fileName,
content: fileContent,