-
Notifications
You must be signed in to change notification settings - Fork 0
/
mizaru.js
77 lines (63 loc) · 1.98 KB
/
mizaru.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
const nodemailer = require('nodemailer');
const path = require('path');
const fs = require('fs');
// Get a password from https://myaccount.google.com/apppasswords and edit config.json
const configData = fs.readFileSync(path.join(__dirname, 'config.json'));
const config = JSON.parse(configData);
if (!config.email.length) {
console.log("Please edit config.json and add your email address")
process.exit();
// let transporter = nodemailer.createTransport({
// sendmail: true,
// newline: 'unix',
// path: '/usr/sbin/sendmail'
// });
}
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: config.email,
pass: config.password
}
});
// create reusable transporter object using the default SMTP transport
const emailData = {
from: `"Mizaru" <${config.email}>`,
to: config.email,
};
let inputBuffer = '';
let timeoutId;
process.stdin.setEncoding('utf8');
console.log("\nMIZARU:\nListening for text input. Control-C to send.");
console.log("Sending to ", config.email)
process.stdin.on('data', (input) => {
clearTimeout(timeoutId);
inputBuffer += input;
timeoutId = setTimeout(sendEmail, 10000); // 10 seconds
});
process.stdin.on('keypress', function (ch, key) {
console.log('got "keypress"', key.name);
if (key && key.ctrl && key.name == 'enter') { // Ctrl + Enter
sendEmail();
}
});
process.on('SIGINT', function() {
console.log('Ctrl+C pressed, sending email...');
sendEmail();
});
function sendEmail() {
console.log("Sending text:\n", inputBuffer)
if (!inputBuffer) return;
let text = inputBuffer.trim()
emailData.subject = text.split('\n').shift(); // first line is the subject
emailData.text = text; // join all lines of text
transporter.sendMail(emailData, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// after sending the email, clear the inputBuffer and reset the timeout
inputBuffer = '';
clearTimeout(timeoutId);
});
}