GithubHelp home page GithubHelp logo

tekii / mailparser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nodemailer/mailparser

1.0 6.0 0.0 113 KB

Decode mime formatted e-mails

License: BSD 3-Clause "New" or "Revised" License

JavaScript 100.00%

mailparser's Introduction

mailparser

mailparser is an asynchronous and non-blocking parser for node.js to parse mime encoded e-mail messages. Handles even large attachments with ease - attachments are parsed in chunks that can be saved into disk or sent to database while parsing (tested with 16MB attachments).

mailparser parses raw source of e-mail messages to convert mime-stream into a structured object.

No need to worry about charsets or decoding quoted-printable or base64 data, mailparser (with the help of node-iconv) does all of it for you. All the textual output from mailparser (subject line, addressee names, message body) is always UTF-8.

Installation

npm install mailparser

Usage

Create a new mailparser object

var MailParser = require("mailparser").MailParser,
    mp = new MailParser();

Set up listener for different events

  • Get mail headers as a structured object

    mp.on("headers", function(headers){
        console.log(headers);
    });
    
  • Get mail body as a structured object

    mp.on("body", function(body){
        console.log(body);
    });
    
  • Get info about binary attachment that is about to start streaming

    mp.on("astart", function(id, headers){
        console.log("attachment id" + id + " started");
        console.log(headers);
    });
    
  • Get part of a binary attachment in the form of a Buffer

    mp.on("astream", function(id, buffer){
        console.log("attachment id" + id);
        console.log(buffer);
    });
    
  • Attachment parsing completed

    mp.on("aend", function(id){
        console.log("attachment " + id + " finished");
    });
    

Feed the parser with data

mp.feed(part1_of_the_message);
mp.feed(part2_of_the_message);
mp.feed(part3_of_the_message);
...
mp.feed(partN_of_the_message);

Finish the feeding

mp.end();

Outcome

Parser returns the headers object with "header" event and it's structured like this

{ useMime: true
, contentType: 'multipart/alternative'
, charset: 'us-ascii'
, format: 'fixed'
, multipart: true
, mimeBoundary: 'Apple-Mail-2-1061547935'
, messageId: '[email protected]'
, messageDate: 1286458909000
, receivedDate: 1286743827944
, contentTransferEncoding: '7bit'
, addressesFrom: 
   [ { address: '[email protected]'
     , name: 'Andris Reinman'
     }
   ]
, addressesReplyTo: []
, addressesTo: [ { address: '[email protected]', name: false } ]
, addressesCc: []
, subject: 'Simple test message with special characters like  \u0161 and \u00f5'
, priority: 3
}

Message body is returned with the "body" event and is structured like this

{ bodyText: 'Mail message as plain text',
, bodyHTML: 'Mail message as HTML',
, bodyAlternate: ["list of additional text/* and multipart/* parts of the message"],
, attachments: ["list of attachments"]
}

Attachments are included full size in the body object if the attachments are textual. Binary attachments are sent to the client as a stream that can be saved into disk if needed (events "astream" and "aend"), only the attachment meta-data is included in the body object this way.

See test.js for an actual usage example (parses the source from mail.txt and outputs to console)

node test.js

You need to install node-iconv before running the test!

NB!

Messages with attachments are typically formatted as nested multipart messages. This means that the bodyText and bodyHTML fields might be left blank. Search for an alternate with content-type multipart from the bodyAlternate array and use the bodyText and bodyHTML defined there instead.

Feeding Non-SMTP Data

By default MailParser assumes an SMTP feed with "." at the start of the line replaced with "..". To disable this feature pass the following option to the constructor:

var mp = new MailParser({fix_smtp_escapes: 0});

The default is to fixup those escape sequences, which isn't what you want if you already have a mail file on disk, or have already fixed those escapes up.

LICENSE

BSD

mailparser's People

Contributors

andris9 avatar baudehlo avatar vgrichina avatar bbigras avatar

Watchers

Pablo Jorge Eduardo Rodriguez avatar Marcelo Glezer avatar James Cloos avatar  avatar Joaquin Diaz Trepat avatar Daniel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.