-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
225 lines (211 loc) · 6.47 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var nodemailer = require("nodemailer"),
postmark = require("postmark")
deep = require("deepjs");
deep.errors.Mail = function(msg, report, fileName, lineNum) {
if (typeof msg === 'object')
report = msg;
if (!msg)
msg = "EmailError";
return this.Error(500, msg, report, fileName, lineNum);
};
var closure = {}; // will hold defaultTransporter
deep.mail = function(params, datas, transporter, closeAfter){
var promises = [];
if(transporter && transporter._deep_ocm_)
transporter = transporter();
if(params && params._deep_ocm_)
params = params();
if(datas && datas._deep_ocm_)
datas = datas();
if(typeof params === 'string') // load params through protocols
promises.push(deep.get(params));
if(typeof datas === 'string') // load datas through protocols
promises.push(deep.get(datas));
if(typeof transporter === 'string') // load transporter through protocols
promises.push(deep.get(transporter));
if(promises.length > 0)
return deep.all(promises)
.done(function(results){
if(typeof params === 'string')
params = results.shift();
if(typeof datas === 'string')
datas = results.shift();
if(typeof transporter === 'string')
transporter = results.shift();
if(params && params._deep_ocm_)
params = params();
if(datas && datas._deep_ocm_)
datas = datas();
transporter = transporter || deep.context("mail-transporter") || closure.transporter;
if(!transporter)
return deep.errors.Mail("You need to define an email transporter before sending email.");
if(transporter._deep_ocm_)
transporter = transporter();
return transporter.send(params, datas, closeAfter);
});
transporter = transporter || deep.context("mail-transporter") || closure.transporter;
if(!transporter)
return deep.when(deep.errors.Mail("You need to define an email transporter before sending email."));
return transporter.send(params, datas, closeAfter);
};
deep.Promise._up({
mail : function(params, transporter, closeAfter) {
var func = function(s) {
return deep.mail(params, deep.utils.nodes.val(s), transporter, closeAfter);
};
func._isDone_ = true;
return this._enqueue(func);
}
});
deep.mail.defaultTransporter = function(transporter){
closure.transporter = transporter;
return transporter;
};
function defaultPreparation (params, datas){
params = deep.utils.copy(params);
var promises = [];
// try to interpret 'text' string with datas, then try to retrieve it (if a protocol is present)
if(typeof params.text === "string")
{
if(datas)
params.text = deep.utils.interpret(params.text, datas);
promises.push(deep.get(params.text));
}
// try to interpret 'html' string with datas, then try to retrieve it (if a protocol is present)
if(typeof params.html === "string")
{
if(datas)
params.html = deep.utils.interpret(params.text, datas);
promises.push(deep.get(params.html));
}
// samething for params.to : great for retrieving bunch of adresses from ressources
if(typeof params.to === 'string')
{
if(datas)
params.html = deep.utils.interpret(params.to, datas);
promises.push(deep.get(params.to));
}
if(promises.length > 0) // have something to load
return deep.all(promises)
.done(function(res){
var tmp = res.shift();
if(typeof params.text === "string")
{
// if 'text' is function : use it to render datas
if(tmp._deep_ocm_)
tmp = tmp();
if(typeof tmp === 'function')
params.text = tmp(datas);
else
params.text = tmp; // or use loaded or provided 'text' string
tmp = res.shift();
}
// if 'html' is function : use it to render datas
if(typeof params.html === "string")
{
if(tmp._deep_ocm_)
tmp = tmp();
if(typeof tmp === 'function')
params.html = tmp(datas);
else
params.html = tmp; // or use loaded or provided 'html' string
tmp = res.shift();
}
if(tmp) // 'to' is a string
{
if(tmp._deep_ocm_)
tmp = tmp();
if(typeof tmp === 'function')
params.to = tmp(datas);
else if(params.to)
params.to = tmp;
}
return params; // return final, prepared, params
});
// no loads
if(!params.text) // construct default 'text'
if(datas)
params.text = JSON.stringify(datas);
else
params.text = "no text.";
if(params.text._deep_ocm_)
params.text = params.text();
if(typeof params.text === 'function') // use function to render datas
params.text = params.text(datas);
if(params.html._deep_ocm_)
params.html = params.html();
if(typeof params.html === 'function') // use function to render datas
params.html = params.html(datas);
return params; // return final, prepared, params
};
var recursePromises = function(params){
// look after any promises in 'text','html','to' : if so : wait for them and catch results
if(params.to.then || (params.text && params.text.then) || (params.html && params.html.then))
return deep.all(params.text, params.html, params.to)
.done(function(results){
params.text = results[0];
params.html = results[1];
params.to = results[2];
return params;
});
};
//_____________________________________ TRANSPORTERS
deep.mail.nodemailer = function(type, config){
var transporter = {
handler:nodemailer.createTransport(type, config),
prepare:defaultPreparation,
send:function(params, datas, closeAfter)
{
var self = this;
return deep.when(this.prepare(params, datas))
.done(recursePromises)
.done(function(params){
var def = deep.Deferred();
self.handler.sendMail(params, function(error, success){
if(error)
def.reject(error);
else
def.resolve(success);
if(closeAfter)
self.handler.close();
});
return def.promise();
});
}
};
return transporter;
};
deep.mail.postmark = function(APIKEY){
var transporter = {
handler:postmark(APIKEY),
prepare:defaultPreparation,
send:function(params, datas)
{
var self = this;
return deep.when(this.prepare(params, datas))
.done(recursePromises)
.done(function(params){
// transform params structure from nodemailer style to postmark style
return {
From: params.from,
To: params.to,
Subject: params.subject,
TextBody: params.text,
Attachements:params.attachments
};
})
.done(function(params){
var def = deep.Deferred();
self.handler.send(params, function(error, success){
if(error)
def.reject(error);
else
def.resolve(success);
});
return def.promise();
});
}
};
return transporter;
};
module.exports = deep.mail;