Skip to content

Commit

Permalink
Added 'date' field, bumped version to 0.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Jan 20, 2014
1 parent 514f695 commit 7d1fd80
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -73,13 +73,14 @@ object with parsed e-mail structure as a parameter.
* **from** - an array of parsed `From` addresses - `[{address:'sender@example.com',name:'Sender Name'}]` (should be only one though)
* **to** - an array of parsed `To` addresses
* **cc** - an array of parsed `Cc` addresses
* **bcc** - an array of parsed 'Bcc' addresses
* **bcc** - an array of parsed 'Bcc' addresses
* **subject** - the subject line
* **references** - an array of reference message id values (not set if no reference values present)
* **inReplyTo** - an array of In-Reply-To message id values (not set if no in-reply-to values present)
* **priority** - priority of the e-mail, always one of the following: *normal* (default), *high*, *low*
* **text** - text body
* **html** - html body
* **date** - date field as a `Date()` object. If date could not be resolved or is not found this field is not set. Check the original date string from `headers.date`
* **attachments** - an array of attachments

### Decode a simple e-mail
Expand Down
9 changes: 8 additions & 1 deletion lib/mailparser.js
Expand Up @@ -446,7 +446,10 @@ MailParser.prototype._processHeaderLine = function(pos){
this._currentNode.useMIME = true;
break;
case "date":
this._currentNode.meta.date = new Date(datetime.strtotime(value)*1000 || Date.now());
this._currentNode.meta.date = new Date(value);
if(Object.prototype.toString.call(this._currentNode.meta.date) != "[object Date]" || this._currentNode.meta.date.toString() == "Invalid Date"){
this._currentNode.meta.date = datetime.strtotime(value) && new Date(datetime.strtotime(value) * 1000);
}
break;
case "to":
if(this._currentNode.to && this._currentNode.to.length){
Expand Down Expand Up @@ -978,6 +981,10 @@ MailParser.prototype._processMimeTree = function(){
returnValue.bcc = this.mimeTree.bcc;
}

if(this.mimeTree.meta.date){
returnValue.date = this.mimeTree.meta.date;
}

if(this.mailData.attachments.length){
returnValue.attachments = [];
for(i=0, len=this.mailData.attachments.length; i<len; i++){
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "mailparser",
"description": "Asynchronous and non-blocking parser for mime encoded e-mail messages",
"version": "0.4.0",
"version": "0.4.1",
"author" : "Andris Reinman",
"maintainers":[
{
Expand Down
36 changes: 36 additions & 0 deletions test/mailparser.js
Expand Up @@ -898,6 +898,42 @@ exports["Transfer encoding"] = {
test.deepEqual(mail.from, [{address: 'user@ldkf.com.tw', name: '游采樺'}]);
test.done();
});
},
"Valid Date header": function(test){
var encodedText = "Date: Wed, 08 Jan 2014 09:52:26 -0800\r\n\r\n1cTW3A==",
mail = new Buffer(encodedText, "utf-8");

var mailparser = new MailParser();
mailparser.end(mail);
mailparser.on("end", function(mail){
test.equal(mail.date.toISOString(), "2014-01-08T17:52:26.000Z");
test.equal(mail.headers.date, "Wed, 08 Jan 2014 09:52:26 -0800");
test.done();
});
},
"Invalid Date header": function(test){
var encodedText = "Date: zzzzz\r\n\r\n1cTW3A==",
mail = new Buffer(encodedText, "utf-8");

var mailparser = new MailParser();
mailparser.end(mail);
mailparser.on("end", function(mail){
test.ok(!mail.date);
test.equal(mail.headers.date, "zzzzz");
test.done();
});
},
"Missing Date header": function(test){
var encodedText = "Subject: test\r\n\r\n1cTW3A==",
mail = new Buffer(encodedText, "utf-8");

var mailparser = new MailParser();
mailparser.end(mail);
mailparser.on("end", function(mail){
test.ok(!mail.date);
test.equal(mail.headers.date, undefined);
test.done();
});
}
};

Expand Down

0 comments on commit 7d1fd80

Please sign in to comment.