-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathemail.js
More file actions
70 lines (63 loc) · 2.03 KB
/
email.js
File metadata and controls
70 lines (63 loc) · 2.03 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
64
65
66
67
68
69
70
const nodemailer = require('nodemailer');
const ejs = require('ejs');
const htmlToText = require('html-to-text');
module.exports = class Email {
constructor(user, url) {
this.to = user.email;
this.firstName = user.name.split(' ')[0];
this.from = `Coding Bots<${process.env.EMAIL_FROM}>`;
this.url = url;
}
newTransport() {
if (process.env.NODE_ENV === 'production') {
return nodemailer.createTransport({
service: 'Gmail',
auth: {
user: process.env.GMAIL_USERNAME,
pass: process.env.GMAIL_PASSWORD,
},
});
}
return nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
//to use gmail, activate in gmail "less secure app" option
// for gmail just specify service: 'Gmail', no need of host and port
});
}
async send(template, subject) {
// 1) Render the HTML based on a pug template
const html = await ejs.renderFile(
`${__dirname}/../views/email/${template}.ejs`,
{
firstName: this.firstName,
url: this.url,
subject,
}
);
// 2)Define the email options
const mailOptions = {
from: this.from,
to: this.to,
subject,
html,
text: htmlToText.fromString(html),
// html:
};
// 3) Create transport and send email
await this.newTransport().sendMail(mailOptions);
}
async sendWelcome() {
await this.send('welcome', 'Welcome to the CodingBots Family!');
}
async sendPasswordReset() {
await this.send(
'passwordReset',
'Your password Reset token (valid for only 10 minutes'
);
}
};