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).
If you are in need of this kind of extra functionality, check out andris9's [mimelib](https://github.com/andris9/mimelib) module. Also check out his [mailparser](http://github.com/andris9/mailparser) module, which comes in handy after you fetch() a 'full' raw email message with this module.
* This example fetches the 'date', 'from', 'to', 'subject' message headers and the message structure of all unread messages in the Inbox since May 20, 2010:
* **validity** - A String containing a number that indicates whether the message IDs in this mailbox have changed or not. In other words, as long as this value does not change on future openings of this mailbox, any cached message IDs for this mailbox are still valid.
* **seqno** - An Integer that designates this message's sequence number. This number changes when messages with smaller sequence numbers are deleted for example (see the ImapConnection's 'deleted' event).
* **headers** - An Object containing the headers of the message, **if headers were requested when calling fetch().** Note: Duplicate headers are dealt with by storing the duplicated values in an array keyed on the header name (e.g. { to: ['foo@bar.com', 'bar@baz.com'] }).
* **structure** - An Array containing the structure of the message, **if the structure was requested when calling fetch().** See below for an explanation of the format of this property.
* Events:
* **data**(String) - Emitted for each message body chunk if a message body is being fetched
* **end** - Emitted when the fetch is complete for this message and its properties
* _ImapFetch_ is an Object that emits these events:
* **message**(ImapMessage) - Emitted for each message resulting from a fetch request
* **end** - Emitted when the fetch request is complete
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** Array first.
Additional custom flags may be provided by the server. If available, these will also be listed in the mailbox's **permFlags** Array.
* **deleted**(<_integer_>seqno) - Fires when a message is deleted from another IMAP connection's session. The callback's argument is the *sequence number* (instead of the unique ID) of the message that was deleted. The sequence numbers of all messages higher than this value **MUST** be decremented by 1 in order to stay synchronized with the server and to keep the continuity of the sequence numbers.
* **msgupdate**(<_ImapMessage_>msg) - Fires when a message's flags have changed, generally from another IMAP connection's session. With that in mind, the only available properties in this case will almost always only be 'seqno' and 'flags' (no 'data' or 'end' events will be emitted on the object).
* **delim** - <_string_> - The (top-level) mailbox hierarchy delimiter. If the server does not support mailbox hierarchies and only a flat list, this value will be `false`.
* **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
, delim: '/' // 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,
* **(constructor)**([<_object_>config]) - _ImapConnection_ - Creates and returns a new instance of ImapConnection using the specified configuration object. Valid config properties are:
* **xoauth** - <_string_> - OAuth token for [OAuth authentication](https://sites.google.com/site/oauthgoog/Home/oauthimap) for servers that support it.
* **connect**(<_function_>callback) - _(void)_ - Attempts to connect and log into the IMAP server. The callback has one parameter: the error (falsey if none).
* **logout**(<_function_>callback) - _(void)_ - Closes the connection to the server.
* **openBox**(<_string_>mailboxName[, <_boolean_>openReadOnly=false], <_function_>callback) - _(void)_ - Opens a specific mailbox that exists on the server. mailboxName should include any necessary prefix/path. The callback has two parameters: the error (falsey if none), and the _Box_ object containing information about the newly opened mailbox.
* **closeBox**(<_function_>callback) - _(void)_ - Closes the currently open mailbox. **Any messages marked as Deleted in the mailbox will be removed if the mailbox was NOT opened in read-only mode.** Additionally, logging out or opening another mailbox without closing the current one first will NOT cause deleted messages to be removed. The callback has one parameter: the error (falsey if none).
* **addBox**(<_string_>mailboxName, <_function_>callback) - _(void)_ - Creates a new mailbox on the server. mailboxName should include any necessary prefix/path. The callback has one parameter: the error (falsey if none).
* **delBox**(<_string_>mailboxName, <_function_>callback) - _(void)_ - Removes a specific mailbox that exists on the server. mailboxName should including any necessary prefix/path. The callback has one parameter: the error (falsey if none).
* **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. The callback has two parameters: the error (falsey if none), and the _Box_ object containing information about the newly renamed mailbox. **Note:** Renaming the 'INBOX' mailbox will instead cause all messages in 'INBOX' to be moved to the new mailbox.
* **getBoxes**([<_string_>nsPrefix,] <_function_>callback) - _(void)_ - Obtains the full list of mailboxes. If nsPrefix is not specified, the main personal namespace is used. The callback has two parameters: the error (falsey if none), and an object with the following format (with example values):
```javascript
{ INBOX: // mailbox name
{ attribs: [] // mailbox attributes. An attribute of 'NOSELECT' indicates the mailbox cannot
// be opened
, delim: '/' // hierarchy delimiter for accessing this mailbox's direct children.
, children: null // an object containing another structure similar in format to this top level,
// otherwise null if no children
, parent: null // pointer to parent mailbox, null if at the top level
* **removeDeleted**(<_function_>callback) - _(void)_ - Permanently removes (EXPUNGEs) all messages flagged as Deleted in the mailbox that is currently open. The callback has one parameter: the error (falsey if none). **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).
* **append**(<_mixed_>msgData, [<_object_>options,] <_function_>callback) - _(void)_ - Appends a message to selected mailbox. msgData is a string or Buffer containing an RFC-822 compatible MIME message. Valid options are:
* **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)
The callback has one parameter: the error (falsey if none).
**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 unique ids, conn.seq.fetch() fetches by sequence number(s) instead of unique ids, 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.
* The following message flags are valid types that do not have arguments:
* '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.
* The following are valid types that require a String parseable by JavaScript's Date object OR a Date instance:
* **Note:** 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 above).
* **fetch**(<_mixed_>source, <_object_>options) - _ImapFetch_ - Fetches message(s) in the currently open mailbox. source can be a message ID, a message ID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message IDs and/or message ID ranges. Valid options are:
* **headers** - <_mixed_> - Boolean true fetches all message headers and an array of header names retrieves only those headers. **Default:** true
* **body** - <_mixed_> - Boolean true fetches the entire raw message body. A string containing a valid partID (see _FetchResult_'s structure property) fetches the entire body of that particular part. The string 'full' fetches the entire (unparsed) email message, including the headers. An array can be given to specify a byte range of the content, where the first value is boolean true or a partID and the second value is the byte range. For example, to fetch the first 500 bytes: '0-500'. **Default:** false
* **copy**(<_mixed_>source, <_string_>mailboxName, <_function_>callback) - _(void)_ - Copies message(s) in the currently open mailbox to another mailbox. source can be a message ID, a message ID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message IDs and/or message ID ranges. The callback has one parameter: the error (falsey if none).
* **move**(<_mixed_>source, <_string_>mailboxName, <_function_>callback) - _(void)_ - Moves message(s) in the currently open mailbox to another mailbox. source can be a message ID, a message ID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message IDs and/or message ID ranges. The callback has one parameter: the error (falsey if none). **Note:** The message(s) in the destination mailbox will have a new message ID.
* **addFlags**(<_mixed_>source, <_mixed_>flags, <_function_>callback) - _(void)_ - Adds flag(s) to message(s). source can be a message ID, a message ID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message IDs and/or message ID ranges. flags is either a single flag or an array of flags. The callback has one parameter: the error (falsey if none).
* **delFlags**(<_mixed_>source, <_mixed_>flags, <_function_>callback) - _(void)_ - Removes flag(s) from message(s). source can be a message ID, a message ID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message IDs and/or message ID ranges. flags is either a single flag or an array of flags. The callback has one parameter: the error (falsey if none).
* **addKeywords**(<_mixed_>source, <_mixed_>keywords, <_function_>callback) - _(void)_ - Adds keyword(s) to message(s). source can be a message ID, a message ID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message IDs and/or message ID ranges. keywords is either a single keyword or an array of keywords. The callback has one parameter: the error (falsey if none).
* **delKeywords**(<_mixed_>source, <_mixed_>keywords, <_function_>callback) - _(void)_ - Removes keyword(s) from message(s). source can be a message ID, a message ID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message IDs and/or message ID ranges. keywords is either a single keyword or an array of keywords. The callback has one parameter: the error (falsey if none).
* fetch() will automatically retrieve the thread id, unique message id, and labels with the message properties being 'x-gm-thrid', 'x-gm-msgid', 'x-gm-labels' respectively