forked from lcortess/simple-parse-smtp-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (141 loc) · 5.05 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
"use strict";
const path = require('path');
const nodemailer = require("nodemailer");
const EmailTemplate = require('email-templates').EmailTemplate;
let SimpleParseSmtpAdapter = (adapterOptions) => {
if (!adapterOptions || !adapterOptions.user || !adapterOptions.password || !adapterOptions.host || !adapterOptions.fromAddress ) {
throw 'SimpleParseSMTPAdapter requires user, password, host, and fromAddress';
}
/**
* Creates trasporter for send emails
*/
let transporter = nodemailer.createTransport({
host: adapterOptions.host,
port: adapterOptions.port,
secure: adapterOptions.isSSL,
name: adapterOptions.name || '127.0.0.1',
auth: {
user: adapterOptions.user,
pass: adapterOptions.password
},
tls: {
rejectUnauthorized: adapterOptions.isTlsRejectUnauthorized !== undefined ? adapterOptions.isTlsRejectUnauthorized : true
}
});
/**
* When emailField is defined in adapterOptines return that field
* if not return the field email and if is undefined returns username
*
* @param Parse Object user
* @return String email
*/
let getUserEmail = (user) => {
let email = user.get('email') || user.get('username');
if (adapterOptions.emailField) {
email = user.get(adapterOptions.emailField);
}
return email;
};
/**
* Return an email template with data rendered using email-templates module
* check module docs: https://github.com/niftylettuce/node-email-templates
*
* @param String template path template
* @param Object data object with data for use in template
*/
let renderTemplate = (template, data) => {
let templateDir = template;
let html = new EmailTemplate(templateDir);
return new Promise((resolve, reject) => {
html.render(data, (err, result) => {
if (err) {
console.log(err)
reject(err);
} else {
resolve(result);
}
});
});
};
/**
* Parse use this function by default for sends emails
* @param mail This object contain to address, subject and email text in plain text
* @returns {Promise}
*/
let sendMail = (mail) => {
let mailOptions = {
to: mail.to,
html: mail.text,
subject: mail.subject,
from: adapterOptions.fromAddress
};
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
if(error) {
console.log(error)
reject(error);
} else {
resolve(info);
}
});
});
};
/**
* When this method is available parse use for send email for reset password
* @param data This object contain {appName}, {link} and {user} user is an object parse of User class
* @returns {Promise}
*/
let sendPasswordResetEmail = (data) => {
let mail = {
subject: 'Reset Password',
to: getUserEmail(data.user)
};
if (adapterOptions.templates && adapterOptions.templates.resetPassword) {
return renderTemplate(adapterOptions.templates.resetPassword.template, data).then((result) => {
mail.text = result.html;
mail.subject = adapterOptions.templates.resetPassword.subject;
return sendMail(mail);
}, (e) => {
return new Promise((resolve, reject) => {
console.log(e)
reject(e);
});
});
} else {
mail.text = data.link;
return sendMail(mail);
}
};
/**
* When this method is available parse use for send email for email verification
* @param data This object contain {appName}, {link} and {user} user is an object parse of User class
* @returns {Promise}
*/
let sendVerificationEmail = (data) => {
let mail = {
subject: 'Verify Email',
to: getUserEmail(data.user)
};
if (adapterOptions.templates && adapterOptions.templates.verifyEmail) {
return renderTemplate(adapterOptions.templates.verifyEmail.template, data).then((result) => {
mail.text = result.html;
mail.subject = adapterOptions.templates.verifyEmail.subject;
return sendMail(mail);
}, (e) => {
return new Promise((resolve, reject) => {
console.log(e);
reject(e);
});
});
} else {
mail.text = data.link;
return sendMail(mail);
}
};
return Object.freeze({
sendMail: sendMail,
sendPasswordResetEmail: sendPasswordResetEmail,
sendVerificationEmail: sendVerificationEmail
});
};
module.exports = SimpleParseSmtpAdapter;