Update index.ts
This commit is contained in:
parent
6351cb87ef
commit
7642268606
@ -51,34 +51,39 @@ export const sendEmailWithAttachment = onRequest({
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const mailgun = new Mailgun(formData);
|
try {
|
||||||
const client = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY });
|
|
||||||
|
|
||||||
const options = {
|
const mailgun = new Mailgun(formData);
|
||||||
wordwrap: 130,
|
const client = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY });
|
||||||
};
|
|
||||||
const textMessage = convert(message, options);
|
|
||||||
const fileBuffer = fs.readFileSync(tempFilePath);
|
|
||||||
const attachmentFilename = fileName || path.basename(fileUrl.split('?')[0]);
|
|
||||||
|
|
||||||
const data = {
|
const options = {
|
||||||
from: process.env.MAILGUN_FROM_ADDRESS,
|
wordwrap: 130,
|
||||||
to: toAddress,
|
};
|
||||||
subject: subject,
|
const textMessage = convert(message, options);
|
||||||
text: textMessage,
|
const fileBuffer = fs.readFileSync(tempFilePath);
|
||||||
html: message,
|
const attachmentFilename = fileName || path.basename(fileUrl.split('?')[0]);
|
||||||
attachment: {
|
|
||||||
data: fileBuffer,
|
|
||||||
filename: attachmentFilename,
|
|
||||||
contentType: 'application/pdf',
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const result = await client.messages.create(process.env.MAILGUN_SERVER, data);
|
const data = {
|
||||||
fs.unlinkSync(tempFilePath);
|
from: process.env.MAILGUN_FROM_ADDRESS,
|
||||||
|
to: toAddress,
|
||||||
|
subject: subject,
|
||||||
|
text: textMessage,
|
||||||
|
html: message,
|
||||||
|
attachment: {
|
||||||
|
data: fileBuffer,
|
||||||
|
filename: attachmentFilename,
|
||||||
|
contentType: 'application/pdf',
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
logger.info('Email with attachment from URL sent successfully');
|
const result = await client.messages.create(process.env.MAILGUN_SERVER, data);
|
||||||
response.json({ success: true, result });
|
fs.unlinkSync(tempFilePath);
|
||||||
|
logger.info('Email with attachment from URL sent successfully');
|
||||||
|
response.json({ success: true, result });
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`Error while sending E-mail. Error: ${e}`);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('Error sending email with attachment from URL:', error);
|
logger.error('Error sending email with attachment from URL:', error);
|
||||||
response.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) });
|
response.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) });
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user