An IMAP client module for node.js.
node-imap is an IMAP client module for node.js.
This module does not perform any magic such as auto-decoding of messages/attachments or parsing of email addresses (node-imap leaves all mail header values as-is).
An upgrade guide from node-imap v0.7.x to v0.8.x can be found here.
node.js -- v10.0.0 or newer
An IMAP server to connect to -- tested with gmail
npm install imap………MessageSource can be a single message identifier, a message identifier range (e.g. '2504:2507' or '*' or '2504:*'), an array of message identifiers, or an array of message identifier ranges.
Box is an object representing the currently open mailbox, and has the following properties:
permFlags may be stored for the current session only. Additional server implementation-specific flags may also be available.ImapMessage is an object representing an email message. It consists of:
info properties:attrs properties:ImapFetch is an object representing a fetch() request. It consists of:
seqno is the message's sequence number.A message structure with multiple parts might look something like the following:
…The above structure describes a message having both an attachment and two forms of the message body (plain text and HTML). Each message part is identified by a partID which is used when you want to fetch the content of that part (see fetch()).
The structure of a message with only one part will simply look something like this:
[ { partID: '1',
type: 'text',
subtype: 'plain',
params: { charset: 'ISO-8859-1' },
id: null,
description: null,
encoding: '7BIT',
size: 935,
lines: 46,
md5: null,
disposition: null,
language: null
}
]Therefore, an easy way to check for a multipart message is to check if the structure length is >1.
Lastly, here are the system flags defined by RFC3501 that may be added/removed:
It should be noted however that the IMAP server can limit which flags can be permanently modified for any given message. If in doubt, check the mailbox's permFlags first. Additional custom flags may be provided by the server. If available, these will also be listed in the mailbox's permFlags.
require('imap') returns one object: Connection.
ready() - Emitted when a connection to the server has been made and authentication was successful.
alert(< string >message) - Emitted when the server issues an alert (e.g. "the server is going down for maintenance").
mail(< integer >numNewMsgs) - Emitted when new mail arrives in the currently open mailbox.
expunge(< integer >seqno) - Emitted when a message was expunged externally. seqno is the sequence number (instead of the unique UID) of the message that was expunged. If you are caching sequence numbers, all sequence numbers higher than this value MUST be decremented by 1 in order to stay synchronized with the server and to keep correct continuity.
uidvalidity(< integer >uidvalidity) - Emitted if the UID validity value for the currently open mailbox changes during the current session.
update(< integer >seqno, < object >info) - Emitted when message metadata (e.g. flags) changes externally.
error(< Error >err) - Emitted when an error occurs. The 'source' property will be set to indicate where the error originated from.
close(< boolean >hadError) - Emitted when the connection has completely closed.
end() - Emitted when the connection has ended.
state - string - The current state of the connection (e.g. 'disconnected', 'connected', 'authenticated').
delimiter - string - The (top-level) mailbox hierarchy delimiter. If the server does not support mailbox hierarchies and only a flat list, this value will be falsey.
namespaces - object - Contains information about each namespace type (if supported by the server) with the following properties:
There should always be at least one entry (although the IMAP spec allows for more, it doesn't seem to be very common) in the personal namespace list, with a blank namespace prefix. Each property's array contains objects of the following format (with example values):
…disableAutoDecode to true to disable automatic decoding of MIME encoded-words that may exist in header field values.Note: Message UID ranges are not guaranteed to be contiguous.
(constructor)([< object >config]) - Connection - Creates and returns a new instance of Connection using the specified configuration object. Valid config properties are:
true to enable keepalive with defaults or set to object to enable and configure keepalive behavior: Default: trueidleInterval is checked. Default: 10000true to force use of NOOP keepalive on servers also support IDLE. Default: falseconnect() - (void) - Attempts to connect and authenticate with the IMAP server.
end() - (void) - Closes the connection to the server after all requests in the queue have been sent.
destroy() - (void) - Immediately destroys the connection to the server.
openBox(< string >mailboxName[, < boolean >openReadOnly=false[, < object >modifiers]], < function >callback) - (void) - Opens a specific mailbox that exists on the server. mailboxName should include any necessary prefi
No open issues yet, or sync has not completed.