Skip to content

Instantly share code, notes, and snippets.

@baudev baudev/app.js
Created Mar 10, 2019

Embed
What would you like to do?
TRENDnet events mails to Free Mobile message
/*
* 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
You can’t perform that action at this time.