forked from steemit/condenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendEmail.js
30 lines (26 loc) · 893 Bytes
/
sendEmail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sendgrid from 'sendgrid';
import config from '../config';
const sg = sendgrid(config.sendgrid.key);
export default function sendEmail(template, to, params, from = null) {
const tmpl_id = config.sendgrid.templates[template];
if (!tmpl_id) throw new Error(`can't find template ${template}`);
const request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: {
template_id: tmpl_id,
personalizations: [
{to: [{email: to}],
substitutions: params},
],
from: {email: from || config.sendgrid.from}
}
});
sg.API(request)
.then(response => {
console.log(`sent '${template}' email to '${to}'`, response.statusCode);
})
.catch(error => {
console.error(`failed to send '${template}' email to '${to}'`, error);
});
}