Browse files

handle very large message text or data

  • Loading branch information...
1 parent 8fc92a1 commit 5bc846c2694678794e8a5d44260f7f197bf881b6 eleith committed Jul 25, 2012
Showing with 144,500 additions and 28 deletions.
  1. +1 −9 Readme.md
  2. +3 −3 email.js
  3. +1 −1 package.json
  4. +23 −14 smtp/message.js
  5. +1 −1 smtp/smtp.js
  6. +144,427 −0 test/attachments/smtp.txt
  7. +44 −0 test/message.js
View
10 Readme.md
@@ -1,12 +1,4 @@
-# warning
-
-please be aware that sending a string (large file attachments work fine) over 12k bytes long will crash the node process:
-
-https://github.com/eleith/emailjs/issues?direction=desc&sort=created&state=open
-
-this message will remain till the issue is closed.
-
-# emailjs (v0.3.0) [![Build Status](https://secure.travis-ci.org/eleith/emailjs.png)](http://travis-ci.org/eleith/emailjs)
+# emailjs (v0.3.1) [![Build Status](https://secure.travis-ci.org/eleith/emailjs.png)](http://travis-ci.org/eleith/emailjs)
send emails, html and attachments (files, streams and strings) from node.js to any smtp server
View
6 email.js
@@ -1,3 +1,3 @@
-exports.server = require('./smtp/client');
-exports.message = require('./smtp/message');
-exports.SMTP = require('./smtp/smtp');
+exports.server = require('./smtp/client');
+exports.message = require('./smtp/message');
+exports.SMTP = require('./smtp/smtp');
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "emailjs",
"description": "send text/html emails and attachments (files, streams and strings) from node.js to any smtp server",
- "version": "0.3.0",
+ "version": "0.3.1",
"author": "eleith",
"contributors":["izuzak", "Hiverness", "mscdex"],
"repository":
View
37 smtp/message.js
@@ -378,22 +378,17 @@ var MessageStream = function(message)
var output_base64 = function(data, callback)
{
- var loops = Math.floor(data.length / MIMECHUNK);
- var leftover= Math.abs(data.length - loops*MIMECHUNK);
+ var loops = Math.ceil(data.length / MIMECHUNK);
+ var loop = 0;
- var loop = function(index)
+ while(loop < loops)
{
- if(index < loops)
- output(data.substring(MIMECHUNK * index, MIMECHUNK * (index + 1)) + CRLF, loop, [index + 1]);
-
- else if(leftover)
- output(data.substring(index*MIMECHUNK) + CRLF, callback);
-
- else if(callback)
- callback();
- };
+ output(data.substring(MIMECHUNK * loop, MIMECHUNK * (loop + 1)) + CRLF);
+ loop++;
+ }
- loop(0);
+ if(callback)
+ callback();
};
var output_text = function(message)
@@ -495,9 +490,23 @@ var MessageStream = function(message)
if(callback)
callback.apply(null, args);
}
+ // we can't buffer the data, so ship it out!
else if(bytes > self.buffer.length)
{
- close({message:"internal buffer got too large to handle!"});
+ if(self.bufferIndex)
+ {
+ self.emit('data', self.buffer.toString("utf-8", 0, self.bufferIndex));
+ self.bufferIndex = 0;
+ }
+
+ var loops = Math.ceil(data.length / self.buffer.length);
+ var loop = 0;
+
+ while(loop < loops)
+ {
+ self.emit('data', data.substring(self.buffer.length*loop, self.buffer.length*(loop + 1)));
+ loop++;
+ }
}
else // we need to clean out the buffer, it is getting full
{
View
2 smtp/smtp.js
@@ -20,7 +20,7 @@ var AUTH_METHODS = {PLAIN:'PLAIN', CRAM_MD5:'CRAM-MD5', LOGIN:'LOGIN'};
var TIMEOUT = 5000;
var DEBUG = 0;
-var log = function()
+var log = function()
{
if(DEBUG)
{
View
144,427 test/attachments/smtp.txt
144,427 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
44 test/message.js
@@ -77,6 +77,50 @@ describe("messages", function()
});
});
+ it("very large text message", function(done)
+ {
+ // thanks to jart+loberstech for this one!
+ var message =
+ {
+ subject: "this is a test TEXT message from emailjs",
+ from: "ninjas@gmail.com",
+ to: "pirates@gmail.com",
+ text: fs.readFileSync(path.join(__dirname, "attachments/smtp.txt"))
+ };
+
+ send(email.message.create(message), function(mail)
+ {
+ expect(mail.text).to.equal(message.text + "\n\n");
+ expect(mail.headers.subject).to.equal(message.subject);
+ expect(mail.headers.from).to.equal(message.from);
+ expect(mail.headers.to).to.equal(message.to);
+ done();
+ });
+ });
+
+ it("very large text data", function(done)
+ {
+ var text = "<html><body><pre>" + fs.readFileSync(path.join(__dirname, "attachments/smtp.txt"), "utf-8") + "</pre></body></html>";
+ var message =
+ {
+ subject: "this is a test TEXT+DATA message from emailjs",
+ from: "lobsters@gmail.com",
+ to: "lizards@gmail.com",
+ text: "hello friend if you are seeing this, you can not view html emails. it is attached inline.",
+ attachment: {data:text, alternative:true}
+ };
+
+ send(message, function(mail)
+ {
+ expect(mail.html).to.equal(text);
+ expect(mail.text).to.equal(message.text + "\n");
+ expect(mail.headers.subject).to.equal(message.subject);
+ expect(mail.headers.from).to.equal(message.from);
+ expect(mail.headers.to).to.equal(message.to);
+ done();
+ });
+ });
+
it("html data", function(done)
{
var html = fs.readFileSync(path.join(__dirname, "attachments/smtp.html"), "utf-8");

0 comments on commit 5bc846c

Please sign in to comment.