mailcomposer
Advanced tools
Comparing version 0.1.27 to 0.1.28
@@ -64,2 +64,8 @@ var Stream = require("stream").Stream, | ||
this._message = {}; | ||
/** | ||
* <p>Contains a list of alternatives for text and html body</p> | ||
* @private | ||
*/ | ||
this._alternatives = []; | ||
@@ -199,3 +205,40 @@ /** | ||
/** | ||
* <p>Adds an alternative to the list</p> | ||
* | ||
* <p>Following options are allowed:</p> | ||
* | ||
* <ul> | ||
* <li><b>fileName</b> - filename for the alternative</li> | ||
* <li><b>contentType</b> - content type for the attachmetn (default will be derived from the filename)</li> | ||
* <li><b>cid</b> - Content ID value for inline images</li> | ||
* <li><b>contents</b> - String or Buffer alternative contents</li> | ||
* <li><b>filePath</b> - Path to a file for streaming</li> | ||
* <li><b>streamSource</b> - Stream object for arbitrary streams</li> | ||
* </ul> | ||
* | ||
* <p>One of <code>contents</code> or <code>filePath</code> or <code>stream</code> | ||
* must be specified, otherwise the alternative is not included</p> | ||
* | ||
* @param {Object} alternative Alternative info | ||
*/ | ||
MailComposer.prototype.addAlternative = function(alternative){ | ||
alternative = alternative || {}; | ||
if(!alternative.contentType){ | ||
alternative.contentType = "application/octet-stream"; | ||
} | ||
if(!alternative.contentEncoding){ | ||
alternative.contentEncoding = "base64"; | ||
} | ||
if(alternative.contents){ | ||
this._alternatives.push(alternative); | ||
} | ||
}; | ||
/** | ||
* <p>Adds an attachment to the list</p> | ||
@@ -547,3 +590,4 @@ * | ||
if(this._message.body && this._message.html){ | ||
if([].concat(this._message.body || []).concat(this._message.html || []). | ||
concat(this._alternatives || []).length > 1){ | ||
this._message.useAlternative = true; | ||
@@ -716,4 +760,17 @@ this._message.alternativeBoundary = this._generateBoundary(); | ||
// Alternatives | ||
if(this._alternatives && this._alternatives.length){ | ||
for(i=0, len = this._alternatives.length; i<len; i++){ | ||
node = this._createAlternativeComponent(this._alternatives[i]); | ||
if(currentNode){ | ||
currentNode.childNodes.push(node); | ||
node.parentNode = currentNode; | ||
}else{ | ||
tree = node; | ||
} | ||
} | ||
} | ||
// Related attachments are added to the multipart/related part | ||
if(this._relatedAttachments && this._relatedAttachments){ | ||
if(this._relatedAttachments && this._relatedAttachments.length){ | ||
for(i=0, len = this._relatedAttachments.length; i<len; i++){ | ||
@@ -774,3 +831,3 @@ node = this._createAttachmentComponent(this._relatedAttachments[i]); | ||
/** | ||
* <p>Creates a mime tree node for a text component (plaintext, HTML)</p> | ||
* <p>Creates a mime tree node for an attachment component</p> | ||
* | ||
@@ -787,3 +844,2 @@ * @param {Object} attachment Attachment info for the component | ||
node.contentEncoding = "base64"; | ||
node.useAttachmentType = true; | ||
@@ -819,2 +875,28 @@ if(attachment.fileName){ | ||
/** | ||
* <p>Creates a mime tree node for an alternative text component (ODF/DOC/etc)</p> | ||
* | ||
* @param {Object} alternative Alternative info for the component | ||
* @return {Object} Mime tree node | ||
*/ | ||
MailComposer.prototype._createAlternativeComponent = function(alternative){ | ||
var node = this._createMimeNode(), | ||
contentType = alternative.contentType, | ||
fileName; | ||
node.contentEncoding = alternative.contentEncoding || "base64"; | ||
if(["7bit", "8bit", "binary"].indexOf(node.contentEncoding)>=0){ | ||
node.textFormat = "flowed"; | ||
contentType.push("format=" + node.textFormat); | ||
} | ||
node.headers.push(["Content-Type", contentType]); | ||
node.headers.push(["Content-Transfer-Encoding", node.contentEncoding]); | ||
node.contents = alternative.contents; | ||
return node; | ||
}; | ||
/** | ||
* <p>Creates an empty mime tree node</p> | ||
@@ -821,0 +903,0 @@ * |
{ | ||
"name": "mailcomposer", | ||
"description": "Compose E-Mail messages", | ||
"version": "0.1.27", | ||
"version": "0.1.28", | ||
"author" : "Andris Reinman", | ||
@@ -6,0 +6,0 @@ "maintainers":[ |
@@ -266,2 +266,34 @@ # mailcomposer | ||
### Add alternatives to HTML and text | ||
In addition to text and HTML, any kind of data can be inserted as an alternative content of the main body - for example a word processing document with the same text as in the HTML field. It is the job of the e-mail client to select and show the best fitting alternative to the reader. | ||
Alternatives to text and HTML can be added with `mailcomposer.addAlternative(alternative)` where | ||
`alternative` is an object with alternative (meta)data with the following possible | ||
properties: | ||
* **contents** - String or a Buffer contents for the attachment | ||
* **contentType** - content type for the attachment, if not set will be derived from the `fileName` property | ||
* **contentEncoding** - how the data is encoded, defaults to "base64" | ||
If `contents` is empty, the alternative will be discarded. Other fields are optional. | ||
**Usage example:** | ||
// add HTML "alternative" | ||
mailcomposer.setMessageOption({ | ||
html: "<b>Hello world!</b>" | ||
}); | ||
// add Markdown alternative | ||
mailcomposer.addAlternative({ | ||
contentType: "text/x-web-markdown", | ||
contents: "**Hello world!**" | ||
}); | ||
If the receiving e-mail client can render messages in Markdown syntax as well, it could prefer | ||
to display this alternative as the main content of the message. | ||
Alternatives can be added as many as you want. | ||
### DKIM Signing | ||
@@ -268,0 +300,0 @@ |
@@ -288,2 +288,21 @@ var testCase = require('nodeunit').testCase, | ||
"Add alternative": function(test){ | ||
var mc = new MailComposer(); | ||
mc.addAlternative(); | ||
test.equal(mc._alternatives.length, 0); | ||
mc.addAlternative({contents:"tere tere"}); | ||
test.equal(mc._alternatives.length, 1); | ||
test.equal(mc._alternatives[0].contentType, "application/octet-stream"); | ||
test.equal(mc._alternatives[0].contentEncoding, "base64"); | ||
test.equal(mc._alternatives[0].contents, "tere tere"); | ||
mc.addAlternative({contents:"tere tere", contentType:"text/plain", contentEncoding:"7bit"}); | ||
test.equal(mc._alternatives[1].contentType, "text/plain"); | ||
test.equal(mc._alternatives[1].contentEncoding, "7bit"); | ||
test.done(); | ||
}, | ||
"Add attachment": function(test){ | ||
@@ -1041,2 +1060,80 @@ var mc = new MailComposer(); | ||
}, | ||
"Only alternative": function(test){ | ||
var mc = new MailComposer(); | ||
mc.addAlternative({ | ||
contents: "tere tere" | ||
}); | ||
mc.streamMessage(); | ||
var mp = new MailParser(); | ||
mc.pipe(mp); | ||
mp.on("end", function(mail){ | ||
test.equal(mail.attachments.length, 1) | ||
test.equal(mail.attachments[0].content.toString(), "tere tere") | ||
test.equal(mail.attachments[0].contentType, "application/octet-stream") | ||
test.done(); | ||
}); | ||
}, | ||
"HTML and text and alternative": function(test){ | ||
var mc = new MailComposer(); | ||
mc.setMessageOption({ | ||
html: "<b>test</b>", | ||
body: "test" | ||
}); | ||
mc.addAlternative({ | ||
contentType: "text/plain", | ||
contents: "tere tere" | ||
}); | ||
mc.streamMessage(); | ||
var mp = new MailParser(); | ||
mc.pipe(mp); | ||
mp.on("end", function(mail){ | ||
test.equal(mail.text.trim(), "test"); | ||
test.equal(mail.html.trim(), "<b>test</b>"); | ||
test.equal(mail.alternatives.length, 1) | ||
test.equal(mail.alternatives[0].content, "tere tere") | ||
test.equal(mail.alternatives[0].contentType, "text/plain") | ||
test.done(); | ||
}); | ||
}, | ||
"HTML and text, alternative and attachment": function(test){ | ||
var mc = new MailComposer(); | ||
mc.setMessageOption({ | ||
html: "<b>test</b>", | ||
body: "test" | ||
}); | ||
mc.addAlternative({ | ||
contentType: "text/plain", | ||
contents: "tere tere 1" | ||
}); | ||
mc.addAttachment({ | ||
contents: "tere tere 2" | ||
}); | ||
mc.streamMessage(); | ||
var mp = new MailParser(); | ||
mc.pipe(mp); | ||
mp.on("end", function(mail){ | ||
test.equal(mail.text.trim(), "test"); | ||
test.equal(mail.html.trim(), "<b>test</b>"); | ||
test.equal(mail.alternatives.length, 1) | ||
test.equal(mail.alternatives[0].content, "tere tere 1") | ||
test.equal(mail.alternatives[0].contentType, "text/plain") | ||
test.equal(mail.attachments.length, 1) | ||
test.equal(mail.attachments[0].content.toString(), "tere tere 2") | ||
test.equal(mail.attachments[0].contentType, "application/octet-stream") | ||
test.done(); | ||
}); | ||
}, | ||
"References Header": function(test){ | ||
@@ -1043,0 +1140,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
187778
2824
377