forked from StephenGrider/FullstackReactCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailer.js
More file actions
55 lines (44 loc) · 1.36 KB
/
Mailer.js
File metadata and controls
55 lines (44 loc) · 1.36 KB
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
47
48
49
50
51
52
53
54
55
const sendgrid = require('sendgrid');
const helper = sendgrid.mail;
const keys = require('../config/keys');
class Mailer extends helper.Mail {
constructor({ subject, recipients }, content) {
super();
this.sgApi = sendgrid(keys.sendGridKey);
this.from_email = new helper.Email('no-reply@emaily.com');
this.subject = subject;
this.body = new helper.Content('text/html', content);
this.recipients = this.formatAddresses(recipients);
this.addContent(this.body);
this.addClickTracking();
this.addRecipients();
}
formatAddresses(recipients) {
return recipients.map(({ email }) => {
return new helper.Email(email);
});
}
addClickTracking() {
const trackingSettings = new helper.TrackingSettings();
const clickTracking = new helper.ClickTracking(true, true);
trackingSettings.setClickTracking(clickTracking);
this.addTrackingSettings(trackingSettings);
}
addRecipients() {
const personalize = new helper.Personalization();
this.recipients.forEach(recipient => {
personalize.addTo(recipient);
});
this.addPersonalization(personalize);
}
async send() {
const request = this.sgApi.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: this.toJSON()
});
const response = await this.sgApi.API(request);
return response;
}
}
module.exports = Mailer;