-
Notifications
You must be signed in to change notification settings - Fork 1
/
flaneur-mailer.js
137 lines (123 loc) · 3.27 KB
/
flaneur-mailer.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
/**
API Guides: https://dev.mailjet.com/guides/
API Reference: https://dev.mailjet.com/email-api/v3/
Node.js wrapper: https://github.com/mailjet/mailjet-apiv3-nodejs
MJML documentation: https://mjml.io/documentation/
*/
const colors = require("colors/safe");
class FlaneurMailer {
constructor(mailjetAPIKeyPublic, mailjetAPIKeyPrivate) {
if (
mailjetAPIKeyPublic === undefined ||
mailjetAPIKeyPrivate === undefined
) {
throw new Error(
"mailjetAPIKeyPublic and mailjetAPIKeyPrivate must be set"
);
}
this.Mailjet = require("node-mailjet").connect(
mailjetAPIKeyPublic,
mailjetAPIKeyPrivate
);
this.mjml2html = require("mjml");
}
listMyTemplates(verbose = true) {
const getTemplates = this.Mailjet.get("template");
return getTemplates
.request({ Limit: 100 })
.then(result => {
result.body.Data.forEach(template => {
if (verbose) {
var color = template.OwnerId === 1 ? colors.gray : colors.green;
/* eslint-disable no-console */
console.log(
color(
`ID: ${template.ID}, Name: ${template.Name}, OwnerID: ${
template.OwnerId
}, OwnerType: ${template.OwnerType}`
)
);
/* eslint-enable no-console */
}
});
return result.body.Data;
})
.catch(e => {
throw new Error(e.errorMessage);
});
}
createNewTemplate(templateName) {
const postTemplate = this.Mailjet.post("template");
const templateData = {
Name: templateName
};
return postTemplate
.request(templateData)
.then(result => {
return result.body.Data[0];
})
.catch(e => {
throw new Error(e.errorMessage);
});
}
deleteTemplate(templateId) {
const deleteTemplate = this.Mailjet.delete("template");
return deleteTemplate
.id(templateId)
.request()
.then(_result => {
return true;
})
.catch(e => {
throw new Error(e);
});
}
updateTemplate(templateId, mjmlTemplatePath) {
const fs = require("fs");
const postTemplate = this.Mailjet.post(
`template/${templateId}/detailcontent`
);
const content = fs.readFileSync(mjmlTemplatePath).toString();
const htmlContent = this.mjml2html(content);
return postTemplate
.request({ "Html-part": htmlContent.html })
.then(_result => {
return templateId;
})
.catch(e => {
throw new Error(e.errorMessage);
});
}
sendTemplate(
templateId,
fromEmail,
fromName,
subject,
vars,
recipients,
errorReporting
) {
const send = this.Mailjet.post("send");
const sendOptions = {
FromEmail: fromEmail,
FromName: fromName,
Subject: subject,
"MJ-TemplateID": templateId,
"MJ-TemplateLanguage": true,
"MJ-TemplateErrorReporting": errorReporting,
"MJ-TemplateErrorDeliver": "deliver",
Vars: vars,
Recipients: recipients
};
return send
.request(sendOptions)
.then(result => {
return result.body.Sent;
})
.catch(e => {
throw new Error(e.errorMessage);
});
}
}
module.exports = FlaneurMailer;