mirror of
https://github.com/MailHops/mailhops-imap-listener.git
synced 2025-05-15 09:20:08 -07:00
Initial commit
This commit is contained in:
commit
b5da37b499
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
config.json
|
||||
*.log
|
||||
node_modules
|
22
README.md
Normal file
22
README.md
Normal file
@ -0,0 +1,22 @@
|
||||
# MailHops IMAP listener
|
||||
[www.MailHops.com](https://www.mailhops.com)
|
||||
|
||||
<img src="https://www.mailhops.com/images/logos/logo.png" alt="MailHops logo" title="MailHops" align="right" />
|
||||
|
||||
This app will loop through all the mail in an IMAP account and geo route all messages. You can use the live feed on your mailhops.com account to monitor your incoming messages. Leave it running and it will route incoming messages.
|
||||
|
||||
```sh
|
||||
# install npm modules
|
||||
npm install
|
||||
|
||||
# Add you email connection info in config.json
|
||||
cp config.sample config.json
|
||||
|
||||
# set searchFilter to UNSEEN to only monitor new incoming messages
|
||||
vim config.json
|
||||
|
||||
# start the app
|
||||
npm start
|
||||
```
|
||||
|
||||
<img src="screenshot.png">
|
10
config.sample
Normal file
10
config.sample
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"username": "",
|
||||
"password": "",
|
||||
"host": "",
|
||||
"port": 993,
|
||||
"searchFilter": ["SEEN"]
|
||||
"mailhops": {
|
||||
"api_key": ""
|
||||
}
|
||||
}
|
85
index.js
Normal file
85
index.js
Normal file
@ -0,0 +1,85 @@
|
||||
var MailListener = require("mail-listener2");
|
||||
var MailHops = require("mailhops");
|
||||
var chalk = require('chalk');
|
||||
var logUpdate = require('log-update');
|
||||
var _ = require('lodash');
|
||||
var config = require('./config.json');
|
||||
|
||||
var configuration = {
|
||||
username: "", // imap username
|
||||
password: "", // imap password
|
||||
host: "", // imap host
|
||||
port: 993, // imap port
|
||||
tls: true,
|
||||
connTimeout: 10000, // Default by node-imap
|
||||
authTimeout: 5000, // Default by node-imap,
|
||||
debug: console.log, // Or your custom function with only one incoming argument. Default: null
|
||||
tlsOptions: { rejectUnauthorized: false },
|
||||
mailbox: "INBOX", // mailbox to monitor
|
||||
searchFilter: ["UNSEEN"], // the search filter being used after an IDLE notification has been retrieved
|
||||
markSeen: false, // all fetched email willbe marked as seen and not fetched next time
|
||||
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
|
||||
mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
|
||||
attachments: false, // download attachments as they are encountered to the project directory
|
||||
attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
|
||||
};
|
||||
|
||||
var mhconfiguration = {
|
||||
api_key: "",
|
||||
api_version: 2,
|
||||
app_name: "Node Inbox Report"
|
||||
};
|
||||
|
||||
if(config){
|
||||
configuration = _.merge(configuration,config);
|
||||
}
|
||||
if(config && config.mailhops){
|
||||
mhconfiguration = _.merge(mhconfiguration,config.mailhops);
|
||||
}
|
||||
|
||||
var mailListener = new MailListener(configuration);
|
||||
|
||||
var mailhops = new MailHops(mhconfiguration);
|
||||
|
||||
mailListener.start(); // start listening
|
||||
|
||||
// stop listening
|
||||
//mailListener.stop();
|
||||
|
||||
mailListener.on("server:connected", function(){
|
||||
console.log("imapConnected");
|
||||
});
|
||||
|
||||
mailListener.on("server:disconnected", function(){
|
||||
console.log("imapDisconnected");
|
||||
});
|
||||
|
||||
mailListener.on("error", function(err){
|
||||
console.log(err);
|
||||
});
|
||||
|
||||
mailListener.on("mail", function(mail, seqno, attributes){
|
||||
// do something with mail object including attachments
|
||||
// mail processing code goes here
|
||||
var ips = mailhops.getIPsFromMailParser(mail);
|
||||
if(ips){
|
||||
mailhops.lookup(ips,function(err, res, body){
|
||||
if(err) return logUpdate(`${chalk.red('MailHops Error: '+err)}`);
|
||||
if(body.error && body.error.message) return logUpdate(`${chalk.red('MailHops Error: '+body.error.message)}`);
|
||||
mail.mailHops = body.response;
|
||||
if(typeof mail.mailHops != 'undefined'){
|
||||
let start = mailhops.getStartHop(mail.mailHops.route);
|
||||
let end = mailhops.getEndHop(mail.mailHops.route);
|
||||
logUpdate(`${chalk.bold(mail.from[0].name+' '+mail.from[0].address)}`);
|
||||
logUpdate.done()
|
||||
logUpdate(`${chalk.green( start.city+', '+start.state+' ('+start.countryCode+')' )} -> ${chalk.red( end.city+', '+end.state+' ('+end.countryCode+')')} ${chalk.yellow(Math.round(mail.mailHops.distance.miles)+' mi.')}
|
||||
`);
|
||||
logUpdate.done()
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
mailListener.on("attachment", function(attachment){
|
||||
console.log(attachment.path);
|
||||
});
|
25
package.json
Normal file
25
package.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "mailhops-imap-listener",
|
||||
"version": "1.0.0",
|
||||
"description": "A nodejs app for using MailHops API to test and monitor your IMAP account.",
|
||||
"main": "./index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/mailhops/mailhops-imap-listener"
|
||||
},
|
||||
"keywords": [
|
||||
"MailHops"
|
||||
],
|
||||
"author": "Andrew Van Tassel",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"chalk": "^1.1.3",
|
||||
"log-update": "^1.0.2",
|
||||
"mail-listener2": "^0.2.0",
|
||||
"mailhops": "^2.0.3"
|
||||
}
|
||||
}
|
BIN
screenshot.png
Normal file
BIN
screenshot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
Loading…
x
Reference in New Issue
Block a user