add decls for mailparser

This commit is contained in:
psnider
2015-08-17 20:27:44 +00:00
parent 1f23ad11d2
commit b8d40ffd99
2 changed files with 155 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
import mailparser_mod = require("mailparser");
import MailParser = mailparser_mod.MailParser;
import ParsedMail = mailparser_mod.ParsedMail;
var mailparser = new MailParser();
mailparser.on("headers", function(headers){
console.log(headers.received);
});
mailparser.on("end", function(mail){
mail; // object structure for parsed e-mail
});
// Decode a simple e-mail
// This example decodes an e-mail from a string
var email = "From: 'Sender Name' <sender@example.com>\r\n"+
"To: 'Receiver Name' <receiver@example.com>\r\n"+
"Subject: Hello world!\r\n"+
"\r\n"+
"How are you today?";
// setup an event listener when the parsing finishes
mailparser.on("end", function(mail_object){
console.log("From:", mail_object.from); //[{address:'sender@example.com',name:'Sender Name'}]
console.log("Subject:", mail_object.subject); // Hello world!
console.log("Text body:", mail_object.text); // How are you today?
});
// send the email source to the parser
mailparser.write(email);
mailparser.end();
// Pipe file to MailParser
// This example pipes a readableStream file to MailParser
mailparser = new MailParser();
import fs = require("fs");
mailparser.on("end", function(mail_object){
console.log("Subject:", mail_object.subject);
});
fs.createReadStream("email.eml").pipe(mailparser);
// Attachments
mailparser.on("end", function(mail_object : ParsedMail){
mail_object.attachments.forEach(function(attachment){
console.log(attachment.fileName);
});
});
// Attachment streaming
var mp = new MailParser({
streamAttachments: true
})
mp.on("attachment", function(attachment, mail){
var output = fs.createWriteStream(attachment.generatedFileName);
attachment.stream.pipe(output);
});

86
mailparser/mailparser.d.ts vendored Normal file
View File

@@ -0,0 +1,86 @@
// Type definitions for mailparser v0.5.2
// Project: https://www.npmjs.com/package/mailparser
// Definitions by: Peter Snider <https://github.com/psnider/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path='../node/node.d.ts' />
declare module 'mailparser' {
import WritableStream = NodeJS.WritableStream;
import EventEmitter = NodeJS.EventEmitter;
interface Options {
debug?: boolean; // if set to true print all incoming lines to console
streamAttachments?: boolean; // if set to true, stream attachments instead of including them
unescapeSMTP?: boolean; // if set to true replace double dots in the beginning of the file
defaultCharset?: string; // the default charset for text/plain and text/html content, if not set reverts to Latin-1
showAttachmentLinks?: boolean; // if set to true, show inlined attachment links <a href="cid:...">filename</a>
}
interface EmailAddress {
address: string;
name: string;
}
interface Attachment {
contentType: string;
fileName: string;
contentDisposition: string; // e.g. 'attachment'
contentId: string; // e.g. '5.1321281380971@localhost'
transferEncoding: string; // e.g. 'base64'
length: number; // length of the attachment in bytes
generatedFileName: string; // e.g. 'image.png'
checksum: string; // the md5 hash of the file, e.g. 'e4cef4c6e26037bcf8166905207ea09b'
content: Buffer; // possibly a SlowBuffer
}
// emitted with the 'end' event
interface ParsedMail {
headers: any; // unprocessed headers in the form of - {key: value} - if there were multiple fields with the same key then the value is an array
from: EmailAddress[]; // should be only one though)
to: EmailAddress[];
cc?: EmailAddress[];
bcc?: EmailAddress[];
subject: string; // the subject line
references?: string[]; // an array of reference message id values (not set if no reference values present)
inReplyTo?: string[]; // an array of In-Reply-To message id values (not set if no in-reply-to values present)
priority?: string; // priority of the e-mail, always one of the following: normal (default), high, low
text: string; // text body
html: string; // html body
date?: Date; // If date could not be resolved or is not found this field is not set. Check the original date string from headers.date
attachments?: Attachment[];
}
class MailParser implements WritableStream {
constructor(options? : Options);
on(event : string, callback : (any : any) => void) : void;
// from WritableStream
writable: boolean;
write(buffer: Buffer, cb?: Function): boolean;
write(str: string, cb?: Function): boolean;
write(str: string, encoding?: string, cb?: Function): boolean;
end(): void;
end(buffer: Buffer, cb?: Function): void;
end(str: string, cb?: Function): void;
end(str: string, encoding?: string, cb?: Function): void;
// from EventEmitter
static listenerCount(emitter: EventEmitter, event: string): number;
addListener(event: string, listener: Function): EventEmitter;
on(event: string, listener: Function): EventEmitter;
once(event: string, listener: Function): EventEmitter;
removeListener(event: string, listener: Function): EventEmitter;
removeAllListeners(event?: string): EventEmitter;
setMaxListeners(n: number): void;
listeners(event: string): Function[];
emit(event: string, ...args: any[]): boolean;
}
}