43 lines
975 B
TypeScript
43 lines
975 B
TypeScript
import _ from 'lodash'
|
|
import rek from 'rek'
|
|
import config from '../config.ts'
|
|
|
|
const headers = {
|
|
authorization: `Basic ${Buffer.from(`api:${config.mailgun.key}`).toString('base64')}`,
|
|
'content-type': 'application/x-www-form-urlencoded',
|
|
accept: 'application/json',
|
|
}
|
|
|
|
interface SendMailOptions {
|
|
from?: string
|
|
to?: string
|
|
replyTo?: string
|
|
subject?: string
|
|
html?: string
|
|
}
|
|
|
|
function mapKey(_value: ANY, key: string) {
|
|
return key === 'replyTo' ? 'h:Reply-To' : key
|
|
}
|
|
|
|
const defaults: SendMailOptions = {
|
|
from: `${config.site.title} Robot <${config.site.emails.robot}>`,
|
|
to: config.site.emails.info,
|
|
html: 'some html',
|
|
}
|
|
|
|
export default function sendMail(options: SendMailOptions): Promise<void> {
|
|
return rek.post(
|
|
config.mailgun.url,
|
|
new URLSearchParams(
|
|
Object.assign({}, defaults, 'replyTo' in options ? _.mapKeys(options, mapKey) : options) as Record<
|
|
string,
|
|
string
|
|
>,
|
|
),
|
|
{
|
|
headers,
|
|
},
|
|
)
|
|
}
|