forked from macarthuror/parse-smtp-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
314 lines (281 loc) · 13 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"use strict";
const fs = require("fs")
const path = require('path')
const nodemailer = require("nodemailer")
/**
* Main function to send emails.
*
* @alias SmtpMailAdapter
*
* Required
* @param {Object} mailOptions Parameters from the parse declaration.
* @param {Object} mailOptions.host used to create the Transport frunction of nodemailer.
* @param {Object} mailOptions.port used to create the Transport frunction of nodemailer.
* @param {Object} mailOptions.user used to create the Transport frunction of nodemailer.
* @param {Object} mailOptions.password used to create the Transport frunction of nodemailer.
* @param {Object} mailOptions.fromAddress used to create the Transport frunction of nodemailer.
* Optionals
* /- nodemailer Option -/
* @param {boolean} [secure=false] Deside if you connect with your SMTP server with a secure connection.
* /- sendMail Options -/
* @param {boolean} [template=false] Define if use the default template or a custom one.
* @param {String} [templatePath=""] Path of the custom template.
*
* @param {Object} passwordOptions Object with all the minimum data to customize the password email.
* @param {Object} passwordOptions.btn Text of the action button.
* @param {Object} passwordOptions.body Text of the email body.
* @param {Object} passwordOptions.subject Subject of the password recovery email.
* @param {Object} confirmOptions Object with all the minimum data to customize the confirm email.
* @param {Object} confirmOptions.btn Text of the action button.
* @param {Object} confirmOptions.body Text of the email body.
* @param {Object} confirmOptions.subject Subject of the confirmation email.
* /- Multi Template Options -/
* @param {boolean} [multiTemplate] If is True you can use a different template for email confirmation and password recovery.
* @param {String} [confirmTemplatePath] Path of the template file.
* @param {String} [passwordTemplatePath] Path of the template file.
*
* @param {boolean} [multiLang] When is True the emails can be send in different languages depending on the "lang" colum of the user object.
* @param {Object} [multiLangPass] Object with all the translations for the password recovery email.
* @param {Object} [multiLangConfirm] Object with all the translations for the confirmation email.
*
* @return {Object} returns one or three functions, depending if you are using multiTemplate
*/
var SmtpMailAdapter = mailOptions => {
if (!mailOptions || !mailOptions.host || !mailOptions.port || !mailOptions.fromAddress || !mailOptions.user || !mailOptions.password) {
throw "SMTP mail adapter requires host, port, fromAddress, user and password"
}
var _templates = mailOptions.template || false;
var _templatePath = mailOptions.templatePath || "";
var _multiTemplate = mailOptions.multiTemplate || false;
var _multiLang = mailOptions.multiLang || false;
var transport = nodemailer.createTransport({
host: mailOptions.host,
port: mailOptions.port,
secure: mailOptions.secure || false,
auth: {
user: mailOptions.user,
pass: mailOptions.password
}
});
/**
* Sends the emails with one template for both types (password recovery and email verification).
*
* In the template you can use only 6 variables from Parse.
* - link
* - btn
* - body
* - username
* - appName
* - subject
*
* @since 1.0.0
*
* @alias sendMail
* @memberof SmtpMailAdapter
*
* @see sendMail/MailAdapter
* @link node_modules\parse-server\lib\Adapters\Email\MailAdapter.js
*
* @param {Object} mail From parse, it contains the minimum to send the mail
* @param {String} mail.to Recipient's email
* @param {String} mail.text Email body with the link and username
* @param {String} mail.subject Email subject
*
* @return {type}
*/
var sendMail = mail => {
let link = mail.text.split("it:\n")[1];
let appName = mail.subject.split("for ")[1];
let username = decodeURIComponent(mail.text.split("username=")[1]);
var filePath = "";
var template = "";
const confirmOptions = mailOptions.confirmOptions || {};
const passwordOptions = mailOptions.passwordOptions || {};
let subject = mail.subject.indexOf("Password") !=-1
? passwordOptions.subject || mail.subject
: confirmOptions.subject || mail.subject;
let body = mail.subject.indexOf("Password") !=-1
? passwordOptions.body || "You requested to reset your password"
: confirmOptions.body || "You are being asked to confirm the e-mail address";
let btn = mail.subject.indexOf("Password") !=-1
? passwordOptions.btn || "Reset Password"
: confirmOptions.btn || "Confirm Email";
let options = mail.subject.indexOf("Password") !=-1
? passwordOptions.others || {}
: confirmOptions.others || {};
if (_templates) {
filePath = path.join("./", _templatePath);
template = eval('`' + fs.readFileSync(filePath).toString() + '`');
} else {
filePath = path.join(__dirname, "/templates/main.html");
template = eval('`' + fs.readFileSync(filePath).toString() + '`');
}
var senderOptions = {
from: mailOptions.fromAddress,
to: mail.to,
subject: subject,
html: template
};
return transport.sendMail(senderOptions)
.then(() => {
return
})
.catch(error => {
throw error
});
};
/**
* Summary. (use period)
*
* Description. (use period)
*
* @since 2.0.0
* @access private
*
* @alias sendVerificationEmail
* @memberof SmtpMailAdapter
*
* @see sendMail/MailAdapter
* @link node_modules\parse-server\lib\Adapters\Email\MailAdapter.js
*
* @param {Object} data Data from Parse Server.
* @param {String} data.link Link to verify the email.
* @param {String} data.appName Name of your App.
* @param {ParseUser} data.user The object of the user you want to ferify.
*
* @param {Object} mailOptions.confirmOptions Manimum options to send the email
* @param {Object} mailOptions.confirmOptions.btn Text of the email action button.
* @param {Object} mailOptions.confirmOptions.body Text of the email budy
* @param {Object} mailOptions.confirmOptions.subject Email subject.
*
* @param {String} mailOptions.confirmTemplatePath Path of the template file.
* @param {Boolean} [_multiLang] If it's true you can send the emails in different languages (depending on the lang colum in the _User object).
* @param {Object} [mailOptions.multiLangConfirm] Object with all the translations.
*
* @return
*/
var sendVerificationEmail = data => {
if(!mailOptions.confirmTemplatePath || !mailOptions.confirmOptions) {
throw "You need to add a template for the confirmation emails and pass the options";
} else if(_multiLang && !mailOptions.multiLangConfirm) {
throw "To use multiLang in the templates needs to pass the multiLangPass object with the translations";
} else if(!mailOptions.confirmOptions
|| !mailOptions.confirmOptions.subject
|| !mailOptions.confirmOptions.body
|| !mailOptions.confirmOptions.btn) {
throw "You need to set the 'confirmOptions' object with subject, body and btn"
}
const user = data.user.attributes;
const link = data.link;
const appName = data.appName;
const defOptions = mailOptions.confirmOptions;
const options = mailOptions.passwordOptions.others || {};
const langOptions = mailOptions.multiLangConfirm
? mailOptions.multiLangConfirm[user.lang] : {};
let subject = (_multiLang && typeof langOptions !== 'undefined')
? langOptions.subject
: defOptions.subject
let body = (_multiLang && typeof langOptions !== 'undefined')
? langOptions.body
: defOptions.body
let btn = (_multiLang && typeof langOptions !== 'undefined')
? langOptions.btn
: defOptions.btn
let filePath = path.join("./", mailOptions.confirmTemplatePath);
let template = eval('`' + fs.readFileSync(filePath).toString() + '`');
var senderOptions = {
from: mailOptions.fromAddress,
to: user.email,
subject: subject,
html: template
};
return transport.sendMail(senderOptions)
.then(() => {
return
})
.catch(error => {
throw error
});
};
/**
* Sends an email to recover the password with a template
*
* Description. (use period)
*
* @since 2.0.0
* @access private
*
* @alias sendPasswordResetEmail
* @memberof SmtpMailAdapter
*
* @see sendMail/MailAdapter
* @link node_modules\parse-server\lib\Adapters\Email\MailAdapter.js
*
* @param {Object} data Data from Parse Server.
* @param {String} data.link Link to recover the password.
* @param {String} data.appName Name of your App.
* @param {ParseUser} data.user The object of the user you want to ferify.
*
* @param {Object} mailOptions.passwordOptions Description of optional variable.
* @param {Object} mailOptions.passwordOptions.btn Text of the email action button.
* @param {Object} mailOptions.passwordOptions.body Text of the email budy
* @param {Object} mailOptions.passwordOptions.subject Email subject.
*
* @param {String} mailOptions.passwordTemplatePath Path of the template file.
* @param {Boolean} [_multiLang] If it's true you can send the emails in different languages (depending on the lang colum in the _User object).
* @param {Object} [mailOptions.multiLangPass] Object with all the translations.
*
* @return
*/
var sendPasswordResetEmail = data => {
if(!mailOptions.passwordTemplatePath || !mailOptions.passwordOptions) {
throw "You need to add a template for the password recovery emails";
} else if(_multiLang && !mailOptions.multiLangPass) {
throw "To use multiLang in the templates needs to pass the multiLangPass object with the translations";
} else if(!mailOptions.passwordOptions
|| !mailOptions.passwordOptions.subject
|| !mailOptions.passwordOptions.body
|| !mailOptions.passwordOptions.btn) {
throw "You need to set the 'passwordOptions' object with subject, body and btn"
}
const user = data.user.attributes;
const link = data.link;
const appName = data.appName;
const defOptions = mailOptions.passwordOptions;
const options = mailOptions.passwordOptions.others || {};
const langOptions = mailOptions.multiLangPass
? mailOptions.multiLangPass[user.lang] : {};
let subject = (_multiLang && typeof langOptions !== 'undefined')
? langOptions.subject
: defOptions.subject
let body = (_multiLang && typeof langOptions !== 'undefined')
? langOptions.body
: defOptions.body
let btn = (_multiLang && typeof langOptions !== 'undefined')
? langOptions.btn
: defOptions.btn
let filePath = path.join("./", mailOptions.passwordTemplatePath);
let template = eval('`' + fs.readFileSync(filePath).toString() + '`');
var senderOptions = {
from: mailOptions.fromAddress,
to: user.email,
subject: subject,
html: template
};
return transport.sendMail(senderOptions)
.then(() => {
return
})
.catch(error => {
throw error
});
};
return (_multiTemplate !== true)
? Object.freeze({ sendMail: sendMail })
: Object.freeze({
sendMail: sendMail,
sendVerificationEmail: sendVerificationEmail,
sendPasswordResetEmail: sendPasswordResetEmail
});
}
module.exports = SmtpMailAdapter;