1 | var
|
2 | util = require('util'),
|
3 | path = require('path'),
|
4 | nodemailer = require('nodemailer'),
|
5 |
|
6 | task = require('./base'),
|
7 | dataflows = require ('../');
|
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 | var
|
65 | mailConfig,
|
66 | templatesDir = 'templates/email';
|
67 |
|
68 | if ('project' in dataflows) {
|
69 |
|
70 | mailConfig = dataflows.config.service.mail;
|
71 | }
|
72 |
|
73 |
|
74 | function resolveTemplate (transConf) {
|
75 |
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 | var mailTask = module.exports = function (config) {
|
81 |
|
82 | this.init (config);
|
83 |
|
84 | };
|
85 |
|
86 | util.inherits (mailTask, task);
|
87 |
|
88 | mailTask.prototype.run = function () {
|
89 |
|
90 | var
|
91 | fields = this.fields,
|
92 | recipients = this.recipients,
|
93 | emails = [];
|
94 |
|
95 | if (!recipients || recipients.length === 0) {
|
96 | var email = this.checkFields (fields);
|
97 | if (!email) {
|
98 | return;
|
99 | }
|
100 | emails.push (email);
|
101 | } else {
|
102 | for (var recId = 0; recId < recipients.length; recId ++) {
|
103 | var email = this.checkFields (recipients[recId], fields);
|
104 | if (!email) {
|
105 | return;
|
106 | }
|
107 | emails.push (email);
|
108 | }
|
109 | }
|
110 |
|
111 | var transport = this.resolveTransport (this.transport);
|
112 | if (!transport)
|
113 | return;
|
114 |
|
115 | this.transporter = this.createTransport (transport);
|
116 |
|
117 | var sentCount = 0;
|
118 |
|
119 | emails.forEach (function (email, idx) {
|
120 |
|
121 | this.transporter.use ('compile', this.render.bind (this));
|
122 |
|
123 | this.transporter.sendMail (email, function (error, response) {
|
124 | if (error)
|
125 | return this.failed (error);
|
126 |
|
127 | this.emit ('log', 'OK: Email sent to ' + email.to);
|
128 |
|
129 | sentCount ++;
|
130 |
|
131 | if (sentCount === emails.length) {
|
132 | this.completed ();
|
133 | }
|
134 | }.bind (this));
|
135 |
|
136 | }.bind (this));
|
137 |
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | mailTask.prototype.checkFields = function (fields, defaults) {
|
147 |
|
148 | var email = {};
|
149 |
|
150 | if (fields.constructor === String) {
|
151 | fields = {to: fields};
|
152 | }
|
153 |
|
154 | if (defaults)
|
155 | for (var f in defaults) {
|
156 | email[f] = defaults[f];
|
157 | }
|
158 |
|
159 | for (var f in fields) {
|
160 | email[f] = fields[f];
|
161 | }
|
162 |
|
163 | email.to = email.email || email.to;
|
164 | email.from = email.sender || email.from;
|
165 |
|
166 | if (!email.to || !email.from || !email.subject)
|
167 | return this.failed ('from, to and subject must be provided');
|
168 | if (!email.text && !email.html)
|
169 | return this.failed ('text or html template must be provided');
|
170 |
|
171 | return email;
|
172 | }
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | mailTask.prototype.createTransport = function (transport) {
|
182 |
|
183 |
|
184 |
|
185 | if (transport === "test") {
|
186 | return nodemailer.createTransport ({
|
187 | name: 'testsend',
|
188 | version: '1',
|
189 | send: function(data, callback) {
|
190 | callback();
|
191 | }
|
192 | });
|
193 | }
|
194 |
|
195 | if (transport.plugin) {
|
196 | var transPlugin = require (transport.plugin);
|
197 | return nodemailer.createTransport (transPlugin (transport.config));
|
198 | }
|
199 |
|
200 | return nodemailer.createTransport (transport);
|
201 | }
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | mailTask.prototype.resolveTransport = function (transConf) {
|
209 |
|
210 | if (transConf === "test") {
|
211 | return transConf;
|
212 | } else if (transConf.constructor === String) {
|
213 | if (!mailConfig) {
|
214 | return this.failed ("you must supply transport configuration via dataflows.config");
|
215 | }
|
216 |
|
217 | return mailConfig.transports[transConf];
|
218 | }
|
219 |
|
220 | return transConf;
|
221 | }
|
222 |
|
223 |
|
224 | mailTask.prototype.render = function (mail, done) {
|
225 |
|
226 |
|
227 | console.log ("STILL NO RENDERER FOR EMAIL");
|
228 |
|
229 | if (!mail || !mail.data || !mail.data.html || mail.data.text) {
|
230 | return done();
|
231 | }
|
232 |
|
233 | done();
|
234 | }
|
235 |
|
236 | mailTask.prototype._batchSend = function (email, recipients) {
|
237 | var self = this,
|
238 | emailField = self.emailField || "email";
|
239 |
|
240 | console.log('Email setup:', email);
|
241 | console.log('SMTP setup:', mailConfig.SMTP);
|
242 |
|
243 | var sendMail = function (err, email) { self._sendMail(err, email); };
|
244 |
|
245 | for (var i = 0; i < recipients.length; i++) {
|
246 | var recipient = recipients[i];
|
247 | email.to = recipient[emailField] || recipient.email || recipient.to;
|
248 | if (!email.to || email.length < 6 || email.to.indexOf('@')<0) continue;
|
249 | self._render(email, recipient, sendMail);
|
250 | }
|
251 |
|
252 |
|
253 |
|
254 | self.emit('log', 'Emails sent to transport. Actual sending not guaranteed. See further log.');
|
255 | self.completed(true);
|
256 |
|
257 | }
|
258 |
|
259 | mailTask.prototype._sendMail = function (err, email) {
|
260 | var self = this;
|
261 |
|
262 | if (err) return self._err(err, 'warning');
|
263 |
|
264 | self.emit('log', 'Sending email to ' + email.to);
|
265 | transport.sendMail(email, function (error, response) {
|
266 | if (error) self._err(error, 'warning');
|
267 | else self.emit('log', 'OK: Email sent to ' + email.to);
|
268 | });
|
269 | }
|
270 |
|