Skip to main content

Watch mailboxes

info

To watch mailboxes, you must configure an IMAP host and port

You can watch your mailboxes, simply with a little bit of configuration.

Configure watching​

To watch a mailbox, you need to tell to MailboxJS what mailbox to watch.

This is done in the configuration of MailboxJS.

Here is an exemple of a configuration with watching :

/imap-config.js
const config = {
user: 'username@server.domain',
pass: 'password',
imap: {
host: 'imap.server.domain',
port: 993,
},
smtp: {
host: 'smtp.server.domain',
port: 994,
}
logger: false,
logging: true,
mailboxesToWatch: ['INBOX'],
mailboxesToWatchInterval: 1000 * 60,
}

export config;

mailboxesToWatch contains an array of all the mailboxes name's to watch (case sensistive).

mailboxesToWatchInterval is the checking new mails interval in ms.

New mails listener​

Now that you have configured watching, you can use event listeners of MailboxJs object.

There is an exemple of listening INBOX new mails :

import { MailboxJs } from 'mailboxjs';
import { config } from './mbjs-config'; // MailboxJsConfig

function onNewMail(mail) {
// On a new mail -> Logs the mail's subject
console.log(`I've received a mail ! (${mail.subject})`);
}

const mbjs = await new MailboxJs(config).run(); // Connects to IMAP and SMTP server

mbjs.on('mail', onNewMail); // Link onNewMail function to mail event listener

Every time that a new mail is detected, onNewMail function will be triggered.