by jcreigno

nodejs library to listen incoming mail

132 Stars 39 Forks Last release: over 6 years ago (0.2.3) MIT License 79 Commits 7 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


Notify your nodejs scripts of incoming imap mail.



event for each new email in IMAP INBOX.


Start listening new mails :

const notifier = require('mail-notifier');

const imap = { user: "yourimapuser", password: "yourimappassword", host: "", port: 993, // imap port tls: true,// use secure connection tlsOptions: { rejectUnauthorized: false } };

notifier(imap) .on('mail', mail => console.log(mail)) .start();

Keep it running forever :

const n = notifier(imap);
n.on('end', () => n.start()) // session closed
  .on('mail', mail => console.log(mail.from[0].address, mail.subject))

Note: If using a Gmail account, you will need to do two things: 1. Enable IMAP in your Gmail account settings, which is detailed here. 2. Authorize "less secure apps", which you is laid out in "Option 2" here.


$ npm install mail-notifier


notifier(config, customDbg)

The constructor function creates a new

. Parameter provide options needed for imap connection.
  • host
    : imap server host
  • port
    : imap server port number
  • user
    : imap user name
  • password
    : imap password
  • tls
    : need a tle connection to server
  • tlsOptions
    : see
    module options
  • markSeen
    : mark mail as read defaults to true
  • box
    : mail box read from defaults to 'INBOX'
  • search
    : search query defaults to ['UNSEEN']
  • connTimeout
    : Number of milliseconds to wait for a connection to be established. Default: 10000
  • authTimeout
    : Number of milliseconds to wait to be authenticated after a connection has been established. Default: 5000
  • debug
    : function - if set, the function will be called with one argument, a string containing some debug info. Default: debug output if enabled.

Options from node-imap are also avaliable.

For backward compatibility

is supported.

: function - if set, the function will be called with args that contain some mail-notifier debug info. Default: debug output if enabled.


const n = notifier(config, (...args) => {
  const msg = util.format(...args);


Start listening for incomming emails.


Stop listening and close IMAP connection.



Sent when a connection to the server has been made.


Sent on incoming new unread email. The parsed Mail is given as first parameter to the event listener.


Sent when an error occurs with the IMAP connection. The first parameter is the



Sent when the IMAP connection is closed. This usually happens after a

method call.


This module relies heavily on node-imap. For more advanced usage, please consider using it directly.


Debugging is enabled via the visionmedia/debug module.

To enable debug info add

to the DEBUG env variable :
$>DEBUG=mailnotifier node sample/simple-mail-notifier.js

Or to also have imap module debug info :

$>DEBUG=mailnotifier,imap node sample/simple-mail-notifier.js

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.