Skip to content

Commit

Permalink
Added messageId option
Browse files Browse the repository at this point in the history
  • Loading branch information
Andris Reinman committed Mar 27, 2012
1 parent 9fdff73 commit 2b9a3c9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ The following are the possible fields of an e-mail message:
- **headers** - An object of additional header fields `{"X-Key-Name": "key value"}` (NB! values as passed as is, you should do your own encoding to 7bit if needed)
- **attachments** - An array of attachment objects.
- **envelope** - optional SMTP envelope, if auto generated envelope is not suitable
- **messageId** - optional Message-Id value, random value will be generated if not set

All text fields (e-mail addresses, plaintext body, html body) use UTF-8 as the encoding.
Attachments are streamed as binary.
Expand Down Expand Up @@ -355,7 +356,8 @@ Attahcment object consists of the following properties:
* **contents** - String or a Buffer contents for the attachment
* **filePath** - path to a file if you want to stream the file instead of including it (better for larger attachments)
* **streamSource** - Stream object for arbitrary binary streams if you want to stream the contents (needs to support *pause*/*resume*)
* **contentType** - optional content type for the attachment, if not set will be derived from the `fileName` property
* **contentType** - optional content type for the attachment, if not set will be derived from the `fileName` property
* **contentDisposition** - optional content disposition type for the attachment, defaults to "attachment"

One of `contents`, `filePath` or `streamSource` must be specified, if none is
present, the attachment will be discarded. Other fields are optional.
Expand Down
39 changes: 39 additions & 0 deletions lib/engines/stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

// Expose to the world
module.exports = StubTransport;

/**
* <p>Generates a stub Transport object for testing purposes</p>
*
* @constructor
*/
function StubTransport(){}

/**
* <p>Generates a raw e-mail source and returns it with callback</p>
*
* @param {Object} emailMessage MailComposer object
* @param {Function} callback Callback function to run when the e-mail is composed
*/
StubTransport.prototype.sendMail = function(emailMessage, callback) {

var output = "";

// sendmail strips this header line by itself
emailMessage.options.keepBcc = true;

emailMessage.on("data", function(data){
output += (data || "").toString("utf-8");
});

emailMessage.on("error", function(err){
callback(err);
});

emailMessage.on("end", function(){
callback(null, {message: output});
});

emailMessage.streamMessage();

};
11 changes: 9 additions & 2 deletions lib/nodemailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ var Transport = require("./transport").Transport,
* Version constants
*/
var X_MAILER_NAME = "Nodemailer",
X_MAILER_VERSION = "0.3.10; +https://github.com/andris9/Nodemailer";
X_MAILER_VERSION = "0.3.11; +https://github.com/andris9/Nodemailer";

module.exports.X_MAILER_NAME = X_MAILER_NAME;
module.exports.X_MAILER_VERSION = X_MAILER_VERSION;

// Export createTransport method
module.exports.createTransport = function(type, options){
Expand Down Expand Up @@ -214,9 +217,13 @@ Nodemailer.prototype.setModuleHeaders = function(){
this.mailcomposer.addHeader("Date", new Date().toUTCString());

// Message ID
this.mailcomposer.addHeader("Message-Id", "<"+
if(this.options.messageId){
this.mailcomposer.addHeader("Message-Id", "<"+this.options.messageId+">");
}else{
this.mailcomposer.addHeader("Message-Id", "<"+
Date.now()+Math.random().toString(16).substr(1)+"@"+
X_MAILER_NAME+">");
}
};

/**
Expand Down
6 changes: 5 additions & 1 deletion lib/transport.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var SendmailTransport = require("./engines/sendmail"),
SMTPTransport = require("./engines/smtp"),
SESTransport = require("./engines/ses");
SESTransport = require("./engines/ses"),
StubTransport = require("./engines/stub");


// Expose to the world
Expand Down Expand Up @@ -29,6 +30,9 @@ function Transport(type, options){
case "SENDMAIL":
this.transport = new SendmailTransport(this.options);
break;
case "STUB":
this.transport = new StubTransport(this.options);
break;
default:
this.transport = false;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
],
"dependencies": {
"mailcomposer": ">= 0.1.9",
"mailcomposer": ">= 0.1.10",
"simplesmtp": ">= 0.1.15"
},
"devDependencies": {
Expand Down
28 changes: 28 additions & 0 deletions test/nodemailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ exports["General tests"] = {
test.done();
});

},

"Use default Message-Id value": function(test){
var transport = nodemailer.createTransport("Stub"),
mailOptions = {};

transport.sendMail(mailOptions, function(error, response){
test.ifError(error);
var regex = "Message\\-Id:\\s*<[0-9\.a-fA-F]+@"+nodemailer.X_MAILER_NAME.replace(/([\(\)\\\.\[\]\-\?\:\!\{\}])/g, "\\$1")+">";
test.ok(response.message.match(new RegExp(regex)));
test.done();
})
},

"Use custom Message-Id value": function(test){
var transport = nodemailer.createTransport("Stub"),
mailOptions = {
messageId: "ABCDEF"
};

transport.sendMail(mailOptions, function(error, response){
test.ifError(error);
test.ok(response.message.match(/Message\-Id:\s*<ABCDEF>/));
// default not present
var regex = "Message\\-Id:\\s*<[0-9\.a-fA-F]+@"+nodemailer.X_MAILER_NAME.replace(/([\(\)\\\.\[\]\-\?\:\!\{\}])/g, "\\$1")+">";
test.ok(!response.message.match(new RegExp(regex)));
test.done();
})
}
};

Expand Down

0 comments on commit 2b9a3c9

Please sign in to comment.