Skip to content

Commit

Permalink
fixed bug where boundaries caused only the first attachment to be viewed
Browse files Browse the repository at this point in the history
fixed bug where a plain message was not sent if sending attachments and no html alternative
  • Loading branch information
eleith committed Apr 4, 2011
1 parent b47ea22 commit 0748e55
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
21 changes: 10 additions & 11 deletions package.json
@@ -1,16 +1,15 @@
{
"name": "emailjs",
"description": "send text/html emails and attachments from node.js to any smtp server",
"version": "0.1.6",
"author": "eleith",
"contributors" : [
],
"repository": {
"name": "emailjs",
"description": "send text/html emails and attachments from node.js to any smtp server",
"version": "0.1.7",
"author": "eleith",
"contributors": ["izuzak"],
"repository":
{
"type": "git",
"url": "http://github.com/eleith/emailjs.git"
},
"dependencies": {
},
"engine": [ "node >= 0.3.8" ],
"main": "email"
"dependencies": {},
"engine": ["node >= 0.3.8"],
"main": "email"
}
49 changes: 31 additions & 18 deletions smtp/message.js
Expand Up @@ -141,41 +141,45 @@ var MessageStream = function(message)
output_process(function() { output_message(index + 1, boundary); });
};

if(index == -1 && self.message.html)
if(index < 0)
{
self.emit('data', ["--", boundary, CRLF].join(""));
output_process(function() { output_alternatives(next); });
}
else if(index == -1 && !self.message.html)
{
next();

if(self.message.html)
{
output_process(function() { output_alternatives(next); });
}
else
{
output_text(next);
}
}
else if(index >= 0 && index < self.message.attachments.length)
else if(index < self.message.attachments.length)
{
self.emit('data', ["--", boundary, CRLF].join(""));
output_process(function() { output_attachment(self.message.attachments[index], next); });
}
else
{
self.emit('data', [CRLF, CRLF, "--", boundary, "--", CRLF, CRLF].join(""));
self.emit('data', ["--", boundary, "--", CRLF, CRLF].join(""));
self.emit('end');
}
};

var output_alternatives = function(next)
{
var boundary = generate_boundary();
var data = ["Content-Type: multipart/alternative; boundary=\"", boundary, "\"", CRLF, CRLF];

data = data.concat(["--", boundary, CRLF]);
data = data.concat(["Content-Type:", self.message.content, CRLF, "Content-Transfer-Encoding: 7bit", CRLF, "Content-Disposition: inline", CRLF, CRLF]);
data = data.concat([self.message.text, CRLF, CRLF]);
self.emit('data', ["Content-Type: multipart/alternative; boundary=\"", boundary, "\"", CRLF, CRLF].join(""));
self.emit('data', ["--", boundary, CRLF].join(""));

output_text(function(){});

var data = ["--", boundary, CRLF];

data = data.concat(["--", boundary, CRLF]);
data = data.concat(["Content-Type:text/html; charset=", self.message.html.charset, CRLF, "Content-Transfer-Encoding: base64", CRLF]);
data = data.concat(["Content-Disposition: inline", CRLF, CRLF]);
data = data.concat([(new Buffer(self.message.html.message)).toString("base64"), CRLF, CRLF]);

data = data.concat(["--", boundary, "--", CRLF, CRLF]);

self.emit('data', data.join(""));
Expand Down Expand Up @@ -215,7 +219,8 @@ var MessageStream = function(message)
else
{
self.emit('data', buffer.slice(0, bytes).toString("base64"));
fs.close(fd, function() { self.emit('end') });
self.emit('data', [CRLF, CRLF].join("")); // important!
fs.close(fd, next);
}
}
else
Expand All @@ -234,6 +239,16 @@ var MessageStream = function(message)
fs.open(attachment.path, 'r+', opened);
};

var output_text = function(next)
{
var data = ["Content-Type:", self.message.content, CRLF, "Content-Transfer-Encoding: 7bit", CRLF];
data = data.concat(["Content-Disposition: inline", CRLF, CRLF]);
data = data.concat([self.message.text || "", CRLF, CRLF]);

self.emit('data', data.join(""));
next();
};

var output_data = function()
{
// are there attachments or alternatives?
Expand All @@ -246,9 +261,7 @@ var MessageStream = function(message)
// otherwise, you only have a text message
else
{
self.emit("Content-Type:" + self.content + CRLF);
self.emit('data', CRLF + self.message.text);
self.emit('end');
output_text(function() { self.emit('end') });
}
};

Expand Down

0 comments on commit 0748e55

Please sign in to comment.