2022-01-02 00:08:52 +00:00
|
|
|
import * as mailgun from 'mailgun-js'
|
2022-08-16 05:13:38 +00:00
|
|
|
import { tryOrLogError } from './utils'
|
2022-01-02 00:08:52 +00:00
|
|
|
|
2022-06-04 21:39:25 +00:00
|
|
|
const initMailgun = () => {
|
|
|
|
const apiKey = process.env.MAILGUN_KEY as string
|
|
|
|
return mailgun({ apiKey, domain: 'mg.manifold.markets' })
|
|
|
|
}
|
2022-01-02 00:08:52 +00:00
|
|
|
|
2022-08-16 05:13:38 +00:00
|
|
|
export const sendTextEmail = async (
|
|
|
|
to: string,
|
|
|
|
subject: string,
|
2022-08-16 16:43:51 +00:00
|
|
|
text: string,
|
|
|
|
options?: Partial<mailgun.messages.SendData>
|
2022-08-16 05:13:38 +00:00
|
|
|
) => {
|
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>',
|
2022-01-02 00:08:52 +00:00
|
|
|
to,
|
|
|
|
subject,
|
|
|
|
text,
|
2022-02-08 11:26:33 +00:00
|
|
|
// Don't rewrite urls in plaintext emails
|
|
|
|
'o:tracking-clicks': 'htmlonly',
|
2022-01-02 00:08:52 +00:00
|
|
|
}
|
2022-08-16 05:13:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-16 05:13:38 +00:00
|
|
|
export const sendTemplateEmail = async (
|
2022-01-10 22:07:44 +00:00
|
|
|
to: string,
|
|
|
|
subject: string,
|
|
|
|
templateId: string,
|
2022-02-23 01:35:25 +00:00
|
|
|
templateData: Record<string, string>,
|
2022-08-04 18:03:02 +00:00
|
|
|
options?: Partial<mailgun.messages.SendTemplateData>
|
2022-01-10 22:07:44 +00:00
|
|
|
) => {
|
2022-08-04 18:03:02 +00:00
|
|
|
const data: mailgun.messages.SendTemplateData = {
|
|
|
|
...options,
|
2022-02-23 01:35:25 +00:00
|
|
|
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
|
|
|
}
|
2022-08-16 05:13:38 +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
|
2022-01-02 00:08:52 +00:00
|
|
|
}
|