Created
March 10, 2019 23:53
-
-
Save baudev/bd6c33ad59e77b3e9f16a3a79a8bb31e to your computer and use it in GitHub Desktop.
TRENDnet events mails to Free Mobile message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Email and Free mobile credentials. | |
*/ | |
let config = { | |
free_user: xxxxxxx, | |
free_password: "XXXXXXX", | |
username: "xxxxxxxxxxxxxx", | |
password: "xxxxxxxxxxxxxx", | |
host: "imap.gmail.com", | |
port: 993, // imap port | |
tls: true, | |
}; | |
/* | |
* !!!!!!!!!!!!!!!!!!!!!!!!!!! | |
* DO NOT EDIT FOLLOWING LINES, EXCEPT THE MESSAGE AT LINE 66! | |
* !!!!!!!!!!!!!!!!!!!!!!!!!!! | |
*/ | |
var MailListener = require("mail-listener2"); | |
const freemobile = require('freemobile-sms'); | |
const publicIp = require('public-ip'); | |
/* | |
* We make all new messages incoming fetched as read. | |
*/ | |
config = { | |
...config, | |
connTimeout: 10000, | |
authTimeout: 5000, | |
debug: console.log, | |
tlsOptions: { rejectUnauthorized: false }, | |
mailbox: "INBOX", | |
searchFilter: ["UNSEEN"], | |
markSeen: true, | |
fetchUnreadOnStart: false, | |
mailParserOptions: {streamAttachments: true}, | |
attachments: false, | |
attachmentOptions: { directory: "attachments/" } | |
}; | |
/* | |
* For Free Mobile dependency | |
*/ | |
var credentials = { | |
user: config.free_user, | |
password: config.free_password | |
}; | |
var mailListener = new MailListener(config); | |
mailListener.start(); // start listening | |
mailListener.on("server:connected", function(){ | |
console.log("imapConnected"); | |
}); | |
mailListener.on("mail", async function (mail, seqno, attributes) { | |
// when a mail is received | |
console.log("emailParsed", mail); | |
if (mail.from[0].address === config.username) { | |
console.log("Alert has been raised!"); | |
var ip = await publicIp.v4(); // we get the public IP address | |
freemobile.send('Alarm. Something weird is happening. Date: \n' + mail.date + '\n Admin interface: ' + ip, credentials) | |
.then(() => { | |
console.log("Message sent!"); | |
}) | |
.catch(error => { | |
console.error('An error has occurred while sending the message.'); | |
}); | |
} | |
}); | |
mailListener.on("error", function(err){ | |
}); | |
mailListener.on("server:disconnected", function(){ | |
console.log("imapDisconnected"); | |
}); | |
mailListener.on("attachment", function(attachment){ | |
console.log(attachment.path); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment