imap-simple
A library providing a simpler interface for common use cases of node-imap, a robust imap client for node.js.
Warning: This library is missing a great deal of functionality from node-imap. If you have functionality you would like to see, we're accepting pull requests!
Examples
Retrieve the subject lines of all unread email
var imaps = ; var config = imap: user: 'your@email.address' password: 'yourpassword' host: 'imap.gmail.com' port: 993 tls: true authTimeout: 3000 ; imaps;
Download all attachments from all unread email since yesterday
var imaps = ; var config = imap: user: 'your@email.address' password: 'yourpassword' host: 'imap.gmail.com' port: 993 tls: true authTimeout: 3000 ; imaps;
API
Exported module
-
connect(<object> options, [<function> callback]) - Promise - Main entry point. Connect to an Imap server. Upon successfully connecting to the Imap server, either calls the provided callback with signature
(err, connection)
, or resolves the returned promise withconnection
, whereconnection
is an instance of ImapSimple. If the connection times out, either the callback will be called with theerr
property set to an instance of ConnectionTimeoutError, or the returned promise will be rejected with the same. Validoptions
properties are:- imap: Options to pass to node-imap constructor 1:1
- connectTimeout: Time in milliseconds to wait before giving up on a connection attempt. (Deprecated: please
use
options.imap.authTimeout
instead)
-
ImapSimple(<object> imap) - ImapSimple - constructor for creating an instance of ImapSimple. Mostly used for testing.
-
errors.ConnectionTimeoutError(<number> timeout) - ConnectionTimeoutError - Error thrown when a connection attempt has timed out.
-
getParts(<Array> struct) - Array - Given the
message.attributes.struct
, retrieve a flattened array ofparts
objects that describe the structure of the different parts of the message's body. Useful for getting a simple list to iterate for the purposes of, for example, finding all attachments.
ImapSimple class
-
openBox(<string> boxName, [<function> callback]) - Promise - Open a mailbox, calling the provided callback with signature
(err, boxName)
, or resolves the returned promise withboxName
. -
search(<object> searchCriteria, [<object> fetchOptions], [<function> callback]) - Promise - Search for and retrieve mail in the previously opened mailbox. The search is performed based on the provided
searchCriteria
, which is the exact same format as node-imap requires. All results will be subsequently downloaded, according to the options provided byfetchOptions
, which are also identical to those passed tofetch
of node-imap. Upon a successful search+fetch operation, either the provided callback will be called with signature(err, results)
, or the returned promise will be resolved withresults
. The format ofresults
is detailed below. See node-imap's ImapMessage signature for information aboutattributes
,which
,size
, andbody
. For any message part that is aHEADER
, the body is automatically parsed into an object.// [{// attributes: object,// parts: [ { which: string, size: number, body: string }, ... ]// }, ...] -
end() - undefined - Close the connection to the imap server.
-
getPartData(<object> message, <object> part, [<function> callback]) - Promise - Downloads part data (which is either part of the message body, or an attachment). Upon success, either the provided callback will be called with signature
(err, data)
, or the returned promise will be resolved withdata
. The data will be automatically decoded based on its encoding. If the encoding of the part is not supported, an error will occur. -
addMessageLabel(<mixed> source, <mixed> label, [<function> callback]) - Promise - Adds the provided label(s) to the specified message(s).
source
corresponds to a node-imap MessageSource which specifies the messages to be moved.label
is either a string or array of strings indicating the labels to add. When completed, either calls the provided callback with signature(err)
, or resolves the returned promise. -
moveMessage(<mixed> source, <string> boxName, [<function> callback]) - Promise - Moves the specified message(s) in the currently open mailbox to another mailbox.
source
corresponds to a node-imap MessageSource which specifies the messages to be moved. When completed, either calls the provided callback with signature(err)
, or resolves the returned promise.
Contributing
Pull requests welcome! This project really needs tests, so those would be very welcome. If you have a use case you want supported, please feel free to add, but be sure to follow the patterns established thus far, mostly:
- support promises AND callbacks
- make your api as simple as possible
- don't worry about exposing implementation details of node-imap when needed
This project is OPEN open source. See CONTRIBUTING.md for more details about contributing.
Semver
This project follows semver. Namely:
- new MAJOR versions when incompatible API changes are made,
- new MINOR versions for backwards-compatible feature additions,
- new PATCH versions for backwards-compatible bug fixes