This repository was archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.js
More file actions
46 lines (40 loc) · 1.28 KB
/
mail.js
File metadata and controls
46 lines (40 loc) · 1.28 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
const nodemailer = require("nodemailer");
let transporter;
function init(config) {
if (!transporter) {
transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: config.gmailUsername,
pass: config.gmailPassword,
},
});
}
}
function createMessageBody(config, marks) {
let body = '<h2>Your latest marks available for confirmation:</h2><hr>'
marks = marks.sort((a, b) => a.value - b.value);
marks.forEach(mark => {
body += `<h3>${mark.name}: ${mark.value}</h3>`;
body += `<h4>${mark.subject}</h4>`;
body += '<br>';
});
body += `<hr><a href="${config.url}">Confirm marks here 👀</a>`;
return body;
}
exports.sendMail = async (config, marks) => {
init(config);
try {
await transporter.sendMail({
from: `"KaschusoNotifier 📢" <${config.gmailUsername}>`,
to: config.emailRecipient,
subject: "You have new marks in your Kaschuso ❗",
html: createMessageBody(config, marks)
});
console.log('Mail sent.');
} catch (e) {
console.log(e);
console.log('Mail failed to send. Check your Gmail credentials');
}
}