-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailService.js
More file actions
63 lines (55 loc) · 1.94 KB
/
EmailService.js
File metadata and controls
63 lines (55 loc) · 1.94 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
const NodeMailer = require('nodemailer')
class EmailService {
constructor(smtpHost, smtpUsername, smtpPassword, smtpPort) {
this.smtpHost = smtpHost;
this.smtpUsername = smtpUsername;
this.smtpPassword = smtpPassword;
this.smtpPort = smtpPort;
}
_smtpConnect() {
return new Promise((resolve) => {
// create reusable transporter object using the default SMTP transport
let transporter = NodeMailer.createTransport({
host: this.smtpHost,
port: this.smtpPort,
secure: false,
tls: { ciphers: 'SSLv3' },
auth: {
user: this.smtpUsername,
pass: this.smtpPassword
}
})
resolve(transporter)
})
}
sendEmail(from, to, subject, body, cc, bcc, replyTo) {
return this._smtpConnect()
.then((transporter) => {
return new Promise((resolve, reject) => {
// setup email data with unicode symbols
let mailOptions = {
from: from, // sender address
to: to, // list of receivers
cc: cc,
bcc: bcc,
replyTo: replyTo,
subject: subject, // Subject line
text: body, // plain text body
html: body // html body
}
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error)
reject(error)
}
else {
console.log('Message sent: %s', info.messageId);
resolve(info)
}
})
})
})
}
}
module.exports = EmailService