fitlien-services/functions/src/email/sendEmail.ts
DhanshCOSQ 0fba4756c7 feature/add-client (#21)
Co-authored-by: Benoy Bose <benoybose@cosq.net>
Reviewed-on: #21
Reviewed-by: Benoy Bose <benoybose@cosq.net>
Co-authored-by: DhanshCOSQ <dhanshas@cosq.net>
Co-committed-by: DhanshCOSQ <dhanshas@cosq.net>
2025-04-21 09:19:03 +00:00

40 lines
1.4 KiB
TypeScript

import { onRequest } from "firebase-functions/v2/https";
import { Request } from "firebase-functions/v2/https";
import { getCorsHandler } from "../shared/middleware";
import { getLogger } from "../shared/config";
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const { convert } = require('html-to-text');
const mailgun = new Mailgun(formData);
const logger = getLogger();
const corsHandler = getCorsHandler();
export const sendEmailMessage = onRequest({
region: '#{SERVICES_RGN}#'
}, (request: Request, response) => {
return corsHandler(request, response, async () => {
const mailGunClient = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY! });
const toAddress = request.body.toAddress;
const subject = request.body.subject;
const message = request.body.message;
const options = {
wordwrap: 130,
};
const textMessage = convert(message, options);
mailGunClient.messages.create(process.env.MAILGUN_SERVER!, {
from: process.env.MAILGUN_FROM_ADDRESS,
to: toAddress,
subject: subject,
text: textMessage,
html: message
}).then((res: any) => {
logger.info(res);
response.send(res);
}).catch((err: any) => {
logger.error(err);
response.send(err);
});
});
});