/
SendgridService.js
46 lines (40 loc) · 1.38 KB
/
SendgridService.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
35
36
37
38
39
40
41
42
43
44
45
46
/*
* @Author: Mike Reich
* @Date: 2016-01-26 12:02:01
* @Last Modified 2016-04-14
*/
'use strict';
import _ from 'underscore';
import moment from 'moment';
import Promise from 'bluebird';
import marked from 'marked';
import SendGrid from 'sendgrid';
const mailerConfig = {}
/**
* A default service for mailing with Mandrill.
*/
export default class SendgridService {
constructor(app) {
this.app = app;
app.get('mailer').service(this)
this.api_username = app.config.SENDGRID_USERNAME || (app.config.sendgrid && app.config.sendgrid.api_username)
this.api_password = app.config.SENDGRID_PASSWORD || (app.config.sendgrid && app.config.sendgrid.api_password)
if(!this.api_username || !this.api_password) throw new Error('No SendGrid credentials specified');
this.client = new SendGrid(this.api_username, this.api_password);
}
sendMessage(to, from, subject, text, opts) {
this.app.log.debug('Sending email via SendGrid to', to)
if(!_.isArray(to)) to = [to]
return Promise.each(to, (t) => {
var message = _.extend({to: t, subject, text, from}, opts);
if(opts.html && text == opts.html) opts.html = marked(opts.html)
return new Promise((resolve, reject) => {
this.client.send(message, (err, json) => {
if(err) console.log(err)
if(err) return reject(err)
return resolve(json)
})
})
})
}
}