diff --git a/functions/src/index.ts b/functions/src/index.ts index 78a31d9..e1101f6 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -51,34 +51,39 @@ export const sendEmailWithAttachment = onRequest({ }); }); - const mailgun = new Mailgun(formData); - const client = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY }); + try { - const options = { - wordwrap: 130, - }; - const textMessage = convert(message, options); - const fileBuffer = fs.readFileSync(tempFilePath); - const attachmentFilename = fileName || path.basename(fileUrl.split('?')[0]); + const mailgun = new Mailgun(formData); + const client = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY }); - const data = { - from: process.env.MAILGUN_FROM_ADDRESS, - to: toAddress, - subject: subject, - text: textMessage, - html: message, - attachment: { - data: fileBuffer, - filename: attachmentFilename, - contentType: 'application/pdf', - } - }; + const options = { + wordwrap: 130, + }; + const textMessage = convert(message, options); + const fileBuffer = fs.readFileSync(tempFilePath); + const attachmentFilename = fileName || path.basename(fileUrl.split('?')[0]); - const result = await client.messages.create(process.env.MAILGUN_SERVER, data); - fs.unlinkSync(tempFilePath); + const data = { + 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'); - response.json({ success: true, result }); + const result = await client.messages.create(process.env.MAILGUN_SERVER, data); + 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) { logger.error('Error sending email with attachment from URL:', error); response.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) });