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); }); }); });