manifold/functions/src/send-email.ts

56 lines
1.5 KiB
TypeScript
Raw Normal View History

import * as mailgun from 'mailgun-js'
import { tryOrLogError } from './utils'
const initMailgun = () => {
const apiKey = process.env.MAILGUN_KEY as string
return mailgun({ apiKey, domain: 'mg.manifold.markets' })
}
export const sendTextEmail = async (
to: string,
subject: string,
2022-08-16 16:43:51 +00:00
text: string,
options?: Partial<mailgun.messages.SendData>
) => {
2022-02-08 11:26:33 +00:00
const data: mailgun.messages.SendData = {
2022-08-16 16:43:51 +00:00
...options,
from: options?.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',
}
const mg = initMailgun().messages()
const result = await tryOrLogError(mg.send(data))
if (result != null) {
console.log('Sent text email', to, subject)
}
return result
2022-01-10 22:07:44 +00:00
}
export const sendTemplateEmail = async (
2022-01-10 22:07:44 +00:00
to: string,
subject: string,
templateId: string,
templateData: Record<string, string>,
options?: Partial<mailgun.messages.SendTemplateData>
2022-01-10 22:07:44 +00:00
) => {
const data: mailgun.messages.SendTemplateData = {
...options,
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),
2022-08-12 16:33:02 +00:00
'o:tag': templateId,
'o:tracking': true,
2022-01-10 22:07:44 +00:00
}
const mg = initMailgun().messages()
const result = await tryOrLogError(mg.send(data))
if (result != null) {
console.log('Sent template email', templateId, to, subject)
}
return result
}