manifold/functions/src/send-email.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

import * as mailgun from 'mailgun-js'
import * as functions from 'firebase-functions'
const DOMAIN = 'mg.manifold.markets'
const mg = mailgun({ apiKey: functions.config().mailgun.key, domain: DOMAIN })
2022-01-10 22:07:44 +00:00
export const sendTextEmail = (to: string, subject: string, text: string) => {
2022-02-08 11:26:33 +00:00
const data: mailgun.messages.SendData = {
from: 'Manifold Markets <info@manifold.markets>',
to,
subject,
text,
2022-02-08 11:26:33 +00:00
// Don't rewrite urls in plaintext emails
'o:tracking-clicks': 'htmlonly',
}
2022-01-10 22:07:44 +00:00
return mg.messages().send(data, (error) => {
if (error) console.log('Error sending email', error)
else console.log('Sent text email', to, subject)
})
}
export const sendTemplateEmail = (
to: string,
subject: string,
templateId: string,
templateData: Record<string, string>,
options?: { from: string }
2022-01-10 22:07:44 +00:00
) => {
const data = {
from: options?.from ?? 'Manifold Markets <info@manifold.markets>',
2022-01-10 22:07:44 +00:00
to,
subject,
template: templateId,
'h:X-Mailgun-Variables': JSON.stringify(templateData),
}
return mg.messages().send(data, (error) => {
if (error) console.log('Error sending email', error)
else console.log('Sent template email', templateId, to, subject)
})
}