Get informed if a new mail arrives in node.js with IMAP

1,097 views
Skip to first unread message

forallinstances forallinstances

unread,
Jan 17, 2014, 7:11:31 AM1/17/14
to nod...@googlegroups.com
I would like to create a module in node.js that will inform me (with an event) if a new mail arrives (using IMAP). Until now i have used IMAP module example code but can anyone help me for the next step? Thanks a lot.

var Imap = require('imap'),
    inspect = require('util').inspect;

var imap = new Imap({
  user: 'mygma...@gmail.com',
  password: 'mygmailpassword',
  host: 'imap.gmail.com',
  port: 993,
  tls: true,
  tlsOptions: { rejectUnauthorized: false }
});

function openInbox(cb) {
  imap.openBox('INBOX', true, cb);
}

imap.once('ready', function() {
  openInbox(function(err, box) {
    if (err) throw err;

/* ??? */


 });

});


imap.once('error', function(err) {
  console.log(err);
});

imap.once('end', function() {
  console.log('Connection ended');
});

imap.connect();

Would this be more easy if connecting these two modules?

https://npmjs.org/package/imap

https://npmjs.org/package/mail-notifier

if so, can anyone help me to do this?

Thank you in advance! :)

Bence Dányi

unread,
Jan 17, 2014, 9:40:30 AM1/17/14
to nod...@googlegroups.com
> Would this be more easy if connecting these two modules?
>
> https://npmjs.org/package/imap
> https://npmjs.org/package/mail-notifier

mail-notifier uses imap internally, and it does exactly what you want.

Bence
:wq

forallinstances forallinstances

unread,
Jan 20, 2014, 6:33:34 AM1/20/14
to nod...@googlegroups.com
Dear Bence, thanks a lot for you help.

However, since I am not experienced yet with node, I would like to ask if anyone can provide me some more information regarding the code since my code is not running :(

Thanks in advance!

Bence Dányi

unread,
Jan 20, 2014, 7:37:34 AM1/20/14
to nod...@googlegroups.com
> However, since I am not experienced yet with node, I would like to ask if
> anyone can provide me some more information regarding the code since my code
> is not running :(

```js
var n = requrie('mail-notifier');
var config = { /* your imap config, the same as in your first mail */ };
n(config).on('mail', function(mail) {
// this functions is called when you receive an email, mail is a JSON object
console.log(mail);
});
```

Bence
:wq
Reply all
Reply to author
Forward
0 new messages