forked from saurabhg22/passport-otp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendEmail.js
38 lines (34 loc) · 1.38 KB
/
sendEmail.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
var nodemailer = require('nodemailer');
class EmailService {
constructor(emailInfo) {
this._email = emailInfo.gmail;
this._password = emailInfo.password;
this._subject = (!emailInfo.emailSubject) ? 'OTP Sent from Twilio': emailInfo.emailSubject ;
this._message = (!emailInfo.messageBody) ? '' : emailInfo.messageBody ;
if (!this._email || !this._password)
throw new Error(
'\nPlease provide all the fields of "emailInfo" in the provider.json file.\n Example--------------------------------->\n'
+ '```\n"emailInfo": {\n'
+ '"gmail": "[email protected]",\n'
+ '"password": "xxxxxxxxxxxx",\n'
+ '"emailSubject": "OTP for login to <YOUR APPLICATION NAME>"\n}\n```\n');
this._transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: this._email,
pass: this._password
}
});
}
sendMessage = async function (recipentEmail, OTP) {
let mailOptions = {
from: this._email,
to: recipentEmail,
subject: this._subject,
text: this._message + '. This is your OTP for login: ' + OTP
};
let result = await this._transporter.sendMail(mailOptions);
return result;
}
}
module.exports = EmailService;