-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (90 loc) · 2.69 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
'use strict';
const fs = require('fs');
const email = require('simplyemail');
const nodemailer = require('nodemailer');
const tokens = require('./tokens.json');
/* To create unique subject so inbox doesn't bundle it. */
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
function genId () {
let text = '';
for (let i = 0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
const util = require('util');
console.log('Welke template verzenden?');
process.stdin.on('data', function (text) {
console.log('User gave input: ', util.inspect(text));
text = text.replace(/\W/g, '');
if (text === 'cijfer') {
email.cijfer({
className: 'Nederlands',
classUrl: 'https://app.simplyhomework.nl/class/YfDrLGoRfkRoqNe6E',
settingsUrl: 'https://app.simplyhomework.nl/settings',
grade: '5,4',
passed: false,
average: '6,2',
}).then(function(result){
console.log(result);
sender(result, text);
}, function(error){
console.log(error);
});
} else if (text === 'project') {
email.project({
projectName: 'Miljoenennota',
projectUrl: 'https://app.simplyhomework.nl/class/YfDrLGoRfkRoqNe6E',
settingsUrl: 'https://app.simplyhomework.nl/settings',
personName: 'Henk de Bakker',
}).then(function(result){
console.log(result);
sender(result, text);
}, function(error){
console.log(error);
});
} else if (text === 'html'){
email.html({
title: 'Test HTML email',
body: '<h2>simplySwag</h2>',
settingsUrl: 'https://app.simplyhomework.nl/settings',
}).then(function(result){
console.log(result);
sender(result, text);
}, function(error){
console.log(error);
});
} else {
throw new Error('template not found');
}
});
function sender (result, template) {
/* Send email */
// create reusable transporter object using the default SMTP transport
// gmailUrl: smtps://xxxxx%40gmail.com:[email protected]
const transporter = nodemailer.createTransport(tokens.gmailUrl);
// setup e-mail data with unicode symbols
const mailOptions = {
from: '"Thomas Konings" <[email protected]>', // sender address
to: 'Thomas Konings, [email protected]', // list of receivers
subject: `sH test - ${template} - ${genId()}`,
html: result, // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error != null) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
process.exit();
});
/* Save code */
fs.writeFile('test.html', result, function (err) {
if (err) {
throw err;
}
console.log('Code saved to test.html');
});
}