forked from steemit/condenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendEmail.js
34 lines (30 loc) · 1.07 KB
/
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
31
32
33
34
import sendgrid from 'sendgrid';
import config from 'config';
const sg = sendgrid(config.get('sendgrid.key'));
export default function sendEmail(template, to, params, from = null) {
if (process.env.NODE_ENV !== 'production') {
console.log(`mail: to <${to}>, from <${from}>, template ${template} (not sent due to not production env)`);
return;
}
const tmpl_id = config.get('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.get('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);
});
}