Reading E-Mails

To read e-mails you must have a mail config key in place or to create a plain object as explained in the "Settings" section.

Sample Code

Here's a sample code of a mail reading function. Check out below for further information of each function.

require("inpaas.mail.service").getReader()
    .withConfig("inpaas.test.imap")    
    .withMarkAsRead(true)
    .readInbox([ "unseen" ])
    .then(function onRead(message) {
        // your message is available in the 'message' parameter    
        // return true to delete/read (withMarkAsRead)
        return true;
             
    }, function onError(e) {
        // if any error occurs while reading the message,
        // you will be notified in the 'catch' part of the Promise
        logging.error("catch: {}", e, e);
             
    }).wait();

getReader()

This will return a Mail Reader instance. All attributes below can be invoked directly from the Reader Instance.

withMarkAsRead(boolean)

After reading a message will mark the message as read(true) or not(false). Some e-mail providers already mark messages as read automatically, if you don't want them to be marked as read you must provide the false value so it continues as Unseen in your server.

withMarkAsDeleted(boolean)

After reading a message will mark the message as deleted(true) or not(false).

withParser(fn)

You can also override the mail parser default behavior, for instance if there's any content-type that you wish to handle in a different manner. You can implement the whole MessageParser or you can override methods, see example below.

.withParser(function(impl) {
    // obtain an instance of the default parser implementation
    var parserImpl = require(impl).getParser();
     
    // override one of the methods
    parserImpl.handleUnsupportedContent = function(mms, content) {
        logging.warn("handling unsupported content: {}", content);
    }
     
    // return your parser instance         
    return parserImpl;
})

The functions that could be overridden as described below:

Function

Description

parse(message, indx)

The parse method receives a raw "message" and returns the parsed e-mail message.

handleContent(mms, message, indx)

Handles the content of the message.

handleText(mms, text, contentType)

Handles any text part inside the message (html / text).

handleMultipart(mms, multipart)

Handles the multipart content (loops the parts inside the multipart) and invokes handlePart.

handlePart(mms, part)

Handles parts inside a multipart content.

handleAttachment(mms, part, type)

Handles any attachment.

handleFlags(mms, message)

Handles the message flags.

handleUnsupportedContent(mms, content)

Handles any unsupported content (The default behavior is to throw an exception).

readFolder(folderName, flags)

You can read messages using the readFolder or readInbox methods, the folder name should be provided in plain text. The flags could be sent in plain text or an array of strings, the flags accepted are RECENT, DELETED, UNSEEN or SEEN.

readInbox(flags)

You can read messages using the readFolder or readInbox methods. The flags could be sent in plain text or an array of strings, the flags accepted are RECENT, DELETED, UNSEEN or SEEN.

Promise.then(resolve, reject)

The readFolder and readInbox methods returns a Promise. Check out Promise Documentation Online for further information.

The resolve future is invoked with the message as an argument. The reject future is invoked with the exception. Check out:

.then(function onRead(message) {
    // your message is available in the 'message' parameter    
    // return true to delete/read (withMarkAsRead)
    return true;
             
}, function onError(e) {
    // if any error occurs while reading the message,
    // you will be notified in the 'catch' part of the Promise
    logging.error("catch: {}", e, e);
         
}).wait();

Keep in mind that Promises are executed in async mode. You can use the "wait()" method to wait for the Promise to be fullfilled before your code can continue.

Updated less than a minute ago

Reading E-Mails


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.