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).
* _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.
* **readOnly** - _boolean_ - True if this mailbox was opened in read-only mode. **(Only available with openBox() calls)**
* **newKeywords** - _boolean_ - True if new keywords can be added to messages in this mailbox.
* **uidvalidity** - _integer_ - A 32-bit number that can be used to determine if UIDs in this mailbox have changed since the last time this mailbox was opened.
* **uidnext** - _integer_ - The uid that will be assigned to the next message that arrives at this mailbox.
* **flags** - _array_ - A list of system-defined flags applicable for this mailbox. Flags in this list but *not* in `permFlags` may be stored for the current session only. Additional server implementation-specific flags may also be available.
* **permFlags** - _array_ - A list of flags that can be permanently added/removed to/from messages in this mailbox.
* **persistentUIDs** - _boolean_ - Whether or not this mailbox has persistent UIDs. This should almost always be true for modern mailboxes and should only be false for legacy mail stores where supporting persistent UIDs was not technically feasible.
* **messages** - _object_ - Contains various message counts for this mailbox:
* **total** - _integer_ - Total number of messages in this mailbox.
* **new** - _integer_ - Number of messages in this mailbox having the Recent flag (this IMAP session is the first to see these messages).
* **unseen** - _integer_ - **(Only available with status() calls)** Number of messages in this mailbox not having the Seen flag (marked as not having been read).
* **uid** - _integer_ - A 32-bit ID that uniquely identifies this message within its mailbox.
* **flags** - _array_ - A list of flags currently set on this message.
* **date** - _string_ - The internal server date for the message.
* **struct** - _array_ - The message's body structure **(only set if requested with fetch())**. See below for an explanation of the format of this property.
* **size** - _integer_ - The RFC822 message size **(only set if requested with fetch())**.
* _ImapFetch_ is an object representing a fetch() request. It consists of:
* Events:
* **message**(<_ImapMessage_ >msg, <_integer_ >seqno) - Emitted for each message resulting from a fetch request. `seqno` is the message's sequence number.
* **error**(<_Error_ >err) - Emitted when an error occurred.
* **end**() - Emitted when all messages have been parsed.
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**.
* **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.
* **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.
* **personal** - _array_ - Mailboxes that belong to the logged in user.
* **other** - _array_ - Mailboxes that belong to other users that the logged in user has access to.
* **shared** - _array_ - Mailboxes that are accessible by any logged in user.
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):
```javascript
{ prefix: '', // A string containing the prefix to use to access mailboxes in this namespace
delimiter: '/', // A string containing the hierarchy delimiter for this namespace, or boolean false
// for a flat namespace with no hierarchy
extensions: [ // An array of namespace extensions supported by this namespace, or null if none
// are specified
{ name: 'X-FOO-BAR', // A string indicating the extension name
params: [ 'BAZ' ] // An array of strings containing the parameters for this extension,
* **parseHeader**(<_string_ >rawHeader[, <_boolean_ >disableAutoDecode]) - _object_ - Parses a raw header and returns an object keyed on header fields and the values are Arrays of header field values. Set `disableAutoDecode` to true to disable automatic decoding of MIME encoded-words that may exist in header field values.
* **(constructor)**([<_object_ >config]) - _Connection_ - Creates and returns a new instance of _Connection_ using the specified configuration object. Valid config properties are:
* **user** - _string_ - Username for plain-text authentication.
* **password** - _string_ - Password for plain-text authentication.
* **xoauth** - _string_ - Base64-encoded OAuth token for [OAuth authentication](https://sites.google.com/site/oauthgoog/Home/oauthimap) for servers that support it (See Andris Reinman's [xoauth.js](https://github.com/andris9/inbox/blob/master/lib/xoauth.js) module to help generate this string).
* **xoauth2** - _string_ - Base64-encoded OAuth2 token for [The SASL XOAUTH2 Mechanism](https://developers.google.com/google-apps/gmail/xoauth2_protocol#the_sasl_xoauth2_mechanism) for servers that support it (See Andris Reinman's [xoauth2](https://github.com/andris9/xoauth2) module to help generate this string).
* **host** - _string_ - Hostname or IP address of the IMAP server. **Default:** "localhost"
* **port** - _integer_ - Port number of the IMAP server. **Default:** 143
* **tlsOptions** - _object_ - Options object to pass to tls.connect() **Default:** (none)
* **autotls** - _string_ - Set to 'always' to always attempt connection upgrades via STARTTLS, 'required' only if upgrading is required, or 'never' to never attempt upgrading. **Default:** 'never'
* **connTimeout** - _integer_ - Number of milliseconds to wait for a connection to be established. **Default:** 10000
* **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 prefix/path. `modifiers` is used by IMAP extensions. `callback` has 2 parameters: <_Error_ >err, <_Box_ >mailbox.
* **closeBox**([<_boolean_ >autoExpunge=true, ]<_function_ >callback) - _(void)_ - Closes the currently open mailbox. If `autoExpunge` is true, any messages marked as Deleted in the currently open mailbox will be removed if the mailbox was NOT opened in read-only mode. If `autoExpunge` is false, you disconnect, or you open another mailbox, messages marked as Deleted will **NOT** be removed from the currently open mailbox. `callback` has 1 parameter: <_Error_ >err.
* **addBox**(<_string_ >mailboxName, <_function_ >callback) - _(void)_ - Creates a new mailbox on the server. `mailboxName` should include any necessary prefix/path. `callback` has 1 parameter: <_Error_ >err.
* **delBox**(<_string_ >mailboxName, <_function_ >callback) - _(void)_ - Removes a specific mailbox that exists on the server. `mailboxName` should including any necessary prefix/path. `callback` has 1 parameter: <_Error_ >err.
* **renameBox**(<_string_ >oldMailboxName, <_string_ >newMailboxName, <_function_ >callback) - _(void)_ - Renames a specific mailbox that exists on the server. Both `oldMailboxName` and `newMailboxName` should include any necessary prefix/path. `callback` has 2 parameters: <_Error_ >err, <_Box_ >mailbox. **Note:** Renaming the 'INBOX' mailbox will instead cause all messages in 'INBOX' to be moved to the new mailbox.
* **subscribeBox**(<_string_ >mailboxName, <_function_ >callback) - _(void)_ - Subscribes to a specific mailbox that exists on the server. `mailboxName` should include any necessary prefix/path. `callback` has 1 parameter: <_Error_ >err.
* **unsubscribeBox**(<_string_ >mailboxName, <_function_ >callback) - _(void)_ - Unsubscribes from a specific mailbox that exists on the server. `mailboxName` should include any necessary prefix/path. `callback` has 1 parameter: <_Error_ >err.
* **status**(<_string_ >mailboxName, <_function_ >callback) - _(void)_ - Fetches information about a mailbox other than the one currently open. `callback` has 2 parameters: <_Error_ >err, <_Box_ >mailbox. **Note:** There is no guarantee that this will be a fast operation on the server. Also, do **not** call this on the currently open mailbox.
* **getBoxes**([<_string_ >nsPrefix,] <_function_ >callback) - _(void)_ - Obtains the full list of mailboxes. If `nsPrefix` is not specified, the main personal namespace is used. `callback` has 2 parameters: <_Error_ >err, <_object_ >boxes. `boxes` has the following format (with example values):
* **getSubscribedBoxes**([<_string_ >nsPrefix,] <_function_ >callback) - _(void)_ - Obtains the full list of subscribed mailboxes. If `nsPrefix` is not specified, the main personal namespace is used. `callback` has 2 parameters: <_Error_ >err, <_object_ >boxes. `boxes` has the same format as getBoxes above.
* **expunge**(<_function_ >callback) - _(void)_ - Permanently removes all messages flagged as Deleted in the currently open mailbox. `callback` has 1 parameter: <_Error_ >err. **Note:** At least on Gmail, performing this operation with any currently open mailbox that is not the Spam or Trash mailbox will merely archive any messages marked as Deleted (by moving them to the 'All Mail' mailbox).
* **mailbox** - _string_ - The name of the mailbox to append the message to. **Default:** the currently open mailbox
* **flags** - _mixed_ - A single flag (e.g. 'Seen') or an _array_ of flags (e.g. `['Seen', 'Flagged']`) to append to the message. **Default:** (no flags)
* **date** - _date_ - What to use for message arrival date/time. **Default:** (current date/time)
**All functions below have sequence number-based counterparts that can be accessed by using the 'seq' namespace of the imap connection's instance (e.g. conn.seq.search() returns sequence number(s) instead of UIDs, conn.seq.fetch() fetches by sequence number(s) instead of UIDs, etc):**
* **search**(<_array_ >criteria, <_function_ >callback) - _(void)_ - Searches the currently open mailbox for messages using given criteria. `criteria` is a list describing what you want to find. For criteria types that require arguments, use an _array_ instead of just the string criteria type name (e.g. ['FROM', 'foo@bar.com']). Prefix criteria types with an "!" to negate.
* 'HEADER' - **Requires two string values, with the first being the header name and the second being the value to search for.** If this second string is empty, all messages that contain the given header name will be returned.
* **Note 1:** For the UID-based search (i.e. "conn.search()"), you can retrieve the UIDs for sequence numbers by just supplying an _array_ of sequence numbers and/or ranges as a criteria (e.g. [ '24:29', 19, '66:*' ]).
* **Note 2:** By default, all criterion are ANDed together. You can use the special 'OR' on **two** criterion to find messages matching either search criteria (see example below).
**Note:** You can also prefix `bodies` strings (i.e. 'TEXT', 'HEADER', 'HEADER.FIELDS', and 'HEADER.FIELDS.NOT' for `message/rfc822` messages and 'MIME' for any kind of message) with part ids. For example: '1.TEXT', '1.2.HEADER', '2.MIME', etc.
**Note 2:** 'HEADER*' sections are only valid for parts whose content type is `message/rfc822`, including the root part (no part id).
* **copy**(<_MessageSource_ >source, <_string_ >mailboxName, <_function_ >callback) - _(void)_ - Copies message(s) in the currently open mailbox to another mailbox. `callback` has 1 parameter: <_Error_ >err.
* **move**(<_MessageSource_ >source, <_string_ >mailboxName, <_function_ >callback) - _(void)_ - Moves message(s) in the currently open mailbox to another mailbox. `callback` has 1 parameter: <_Error_ >err. **Note:** The message(s) in the destination mailbox will have a new message UID.
* **addKeywords**(<_MessageSource_ >source, <_mixed_ >keywords, <_function_ >callback) - _(void)_ - Adds keyword(s) to message(s). `keywords` is either a single keyword or an _array_ of keywords. `callback` has 1 parameter: <_Error_ >err.
* **delKeywords**(<_MessageSource_ >source, <_mixed_ >keywords, <_function_ >callback) - _(void)_ - Removes keyword(s) from message(s). `keywords` is either a single keyword or an _array_ of keywords. `callback` has 1 parameter: <_Error_ >err.
* **setKeywords**(<_MessageSource_ >source, <_mixed_ >keywords, <_function_ >callback) - _(void)_ - Sets keyword(s) for message(s). `keywords` is either a single keyword or an _array_ of keywords. `callback` has 1 parameter: <_Error_ >err.
* **setLabels**(<_MessageSource_ >source, <_mixed_ >labels, <_function_ >callback) - _(void)_ - Replaces labels of message(s) with `labels`. `labels` is either a single label or an _array_ of labels. `callback` has 1 parameter: <_Error_ >err.
* **addLabels**(<_MessageSource_ >source, <_mixed_ >labels, <_function_ >callback) - _(void)_ - Adds `labels` to message(s). `labels` is either a single label or an _array_ of labels. `callback` has 1 parameter: <_Error_ >err.
* **delLabels**(<_MessageSource_ >source, <_mixed_ >labels, <_function_ >callback) - _(void)_ - Removes `labels` from message(s). `labels` is either a single label or an _array_ of labels. `callback` has 1 parameter: <_Error_ >err.
* **setQuota**(<_string_ >quotaRoot, <_object_ >quotas, <_function_ >callback) - _(void)_ - Sets the resource limits for `quotaRoot` using the limits in `quotas`. `callback` has 2 parameters: <_Error_ >err, <_object_ >limits. `limits` has the same format as `limits` passed to getQuota()'s callback. Example `quotas` properties (taken from RFC2087):
* **getQuota**(<_string_ >quotaRoot, <_function_ >callback) - _(void)_ - Gets the resource usage and limits for `quotaRoot`. `callback` has 2 parameters: <_Error_ >err, <_object_ >limits. `limits` is keyed on the resource name, where the values are objects with the following properties:
* **getQuotaRoot**(<_string_ >mailbox, <_function_ >callback) - _(void)_ - Gets the list of quota roots for `mailbox` and the resource usage and limits for each. `callback` has 2 parameters: <_Error_ >err, <_object_ >info. `info` is keyed on the quota root name, where the values are objects structured like `limits` given by getQuota(). Example `info`:
* The callback passed to append() will receive an additional argument (the UID of the appended message): <_integer_ >appendedUID.
* The callback passed to append()/seq.append() will receive an additional argument (the UID(s) of the copied message(s) in the destination mailbox): <_mixed_ >newUIDs. `newUIDs` can be an integer if just one message was copied, or a string for multiple messages (e.g. '100:103' or '100,125,130' or '100,200:201').
* **MODSEQ** - _string_ - Modification sequence value. If this criteria is used, the callback parameters are then: <_Error_ >err, <_array_ >UIDs, <_string_ >modseq. The `modseq` callback parameter is the highest modification sequence value of all the messages identified in the search results.
* **addFlagsSince**(<_MessageSource_ >source, <_mixed_ >flags, <_string_ >modseq, <_function_ >callback) - _(void)_ - Adds flag(s) to message(s) that have not changed since `modseq`. `flags` is either a single flag or an _array_ of flags. `callback` has 1 parameter: <_Error_ >err.
* **delFlagsSince**(<_MessageSource_ >source, <_mixed_ >flags, <_string_ >modseq, <_function_ >callback) - _(void)_ - Removes flag(s) from message(s) that have not changed since `modseq`. `flags` is either a single flag or an _array_ of flags. `callback` has 1 parameter: <_Error_ >err.
* **setFlagsSince**(<_MessageSource_ >source, <_mixed_ >flags, <_string_ >modseq, <_function_ >callback) - _(void)_ - Sets the flag(s) for message(s) that have not changed since `modseq`. `flags` is either a single flag or an _array_ of flags. `callback` has 1 parameter: <_Error_ >err.
* **addKeywordsSince**(<_MessageSource_ >source, <_mixed_ >keywords, <_string_ >modseq, <_function_ >callback) - _(void)_ - Adds keyword(s) to message(s) that have not changed since `modseq`. `keywords` is either a single keyword or an _array_ of keywords. `callback` has 1 parameter: <_Error_ >err.
* **delKeywordsSince**(<_MessageSource_ >source, <_mixed_ >keywords, <_string_ >modseq, <_function_ >callback) - _(void)_ - Removes keyword(s) from message(s) that have not changed since `modseq`. `keywords` is either a single keyword or an _array_ of keywords. `callback` has 1 parameter: <_Error_ >err.
* **setKeywordsSince**(<_MessageSource_ >source, <_mixed_ >keywords, <_string_ >modseq, <_function_ >callback) - _(void)_ - Sets keyword(s) for message(s) that have not changed since `modseq`. `keywords` is either a single keyword or an _array_ of keywords. `callback` has 1 parameter: <_Error_ >err.
* **esearch**(<_array_ >criteria, <_array_ >options, <_function_ >callback) - _(void)_ - A variant of search() that can return metadata about results. `callback` has 2 parameters: <_Error_ >err, <_object_ >info. `info` has possible keys: 'all', 'min', 'max', 'count'. Valid `options`:
* 'ALL' - Retrieves UIDs in a compact form (e.g. [2, '10:11'] instead of search()'s [2, 10, 11]) that match the criteria.
* 'MIN' - Retrieves the lowest UID that satisfies the criteria.
* 'MAX' - Retrieves the highest UID that satisfies the criteria.
* 'COUNT' - Retrieves the number of messages that satisfy the criteria.
**Note:** specifying no `options` or [] for `options` is the same as ['ALL']
* **sort**(<_array_ >sortCriteria, <_array_ >searchCriteria, <_function_ >callback) - _(void)_ - Performs a sorted search(). A seqno-based counterpart also exists for this function. `callback` has 2 parameters: <_Error_ >err, <_array_ >UIDs. Valid `sortCriteria` are (reverse sorting of individual criteria is done by prefixing the criteria with '-'):
* 'ARRIVAL' - Internal date and time of the message. This differs from the ON criteria in search(), which uses just the internal date.
* 'CC' - The mailbox of the **first** "cc" address.
* 'DATE' - Message sent date and time.
* 'FROM' - The mailbox of the **first** "from" address.
* 'SIZE' - Size of the message in octets.
* 'SUBJECT' - Base subject text.
* 'TO' - The mailbox of the **first** "to" address.
* Server capability: THREAD=REFERENCES, THREAD=ORDEREDSUBJECT
* **thread**(<_string_ >algorithm, <_array_ >searchCriteria, <_function_ >callback) - _(void)_ - Performs a regular search with `searchCriteria` and groups the resulting search results using the given `algorithm` (e.g. 'references', 'orderedsubject'). `callback` has 2 parameters: <_Error_ >err, <_array_ >UIDs. `UIDs` is a nested array.