-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsender.js
More file actions
122 lines (101 loc) · 2.84 KB
/
Copy pathsender.js
File metadata and controls
122 lines (101 loc) · 2.84 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
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
const emailConfig = require('./config')();
const unirest = require('unirest');
class Sender
{
constructor()
{
this._status = null;
this.status_dev = 'dev';
this.status_prod = 'prod';
this._from = null;
this._to = null;
this._bcc = null;
this._subject = null;
this._text = null;
this._html = " ";
this._template = {
name: null,
variables: {},
};
this._tag = null;
if(emailConfig.status !== this.status_dev && emailConfig.status !== this.status_prod) {
throw new Error('Unknown status for run or not defined in ENV file!');
}
this._status = emailConfig.status;
}
setFrom(mail) {
this._from = mail;
}
setTo(mail) {
this._to = mail;
}
setToWithName(name, email) {
this._to = name + ' <' + email + '>';
}
setBcc(mail) {
this._bcc = mail;
}
setSubject(text) {
this._subject = text;
}
setText(text) {
this._text = text;
}
setTemplate(tpl) {
if(!(tpl instanceof Object)){
console.error("template have to be object");
return;
}
if(!tpl.hasOwnProperty("name")){
console.error("template have to defined name property");
return;
}
if(!tpl.hasOwnProperty("variables")){
console.error("template have to defined variables");
return;
}
if(Object.keys(tpl.variables).length === 0){
console.error("template have to have defined some variables always");
return;
}
this._template = tpl;
}
setTag(tag) {
this._tag = tag;
}
isDevMode(){
return this.status_dev === this._status;
}
//
// override receiver in DEV mode
//
getReceiver() {
return this.isDevMode() ? emailConfig.devMail : this._to;
}
async send(callback)
{
if(this.isDevMode())
{
let prefix = 'DEV - to: ' + this._to;
this._subject = prefix + ' |' + this._subject;
}
await unirest
.post("https://api.mailgun.net/v3/" + emailConfig.domain + "/messages")
.auth({
user: "api",
pass: emailConfig.apiKey,
})
.field("from", this._from)
.field("to", this.getReceiver())
.field("bcc", this._bcc)
.field("subject", this._subject)
.field("text", this._text)
.field("template", this._template.name)
.field("h:X-Mailgun-Variables", JSON.stringify(this._template.variables))
.field("tag", this._tag)
.end((response) => {
callback(response.error, response.body)
});
}
}
module.exports = Sender;