Permalink
Browse files

Added messageId option

  • Loading branch information...
Andris Reinman
Andris Reinman committed Mar 27, 2012
1 parent 9fdff73 commit 2b9a3c9f808dba35259b3559617a74ea30066dab
Showing with 85 additions and 5 deletions.
  1. +3 −1 README.md
  2. +39 −0 lib/engines/stub.js
  3. +9 −2 lib/nodemailer.js
  4. +5 −1 lib/transport.js
  5. +1 −1 package.json
  6. +28 −0 test/nodemailer.js
View
@@ -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.
@@ -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.
View
@@ -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();
+
+};
View
@@ -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){
@@ -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+">");
+ }
};
/**
View
@@ -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
@@ -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;
}
View
@@ -25,7 +25,7 @@
}
],
"dependencies": {
- "mailcomposer": ">= 0.1.9",
+ "mailcomposer": ">= 0.1.10",
"simplesmtp": ">= 0.1.15"
},
"devDependencies": {
View
@@ -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();
+ })
}
};

0 comments on commit 2b9a3c9

Please sign in to comment.