Permalink
Browse files

new test suite, remove need to explicitly call email.message.create

  • Loading branch information...
1 parent 13ecc24 commit 8ee6877e763b9e1beba2a27da5bf1dbc0849282c eleith committed May 28, 2012
Showing with 386 additions and 363 deletions.
  1. +11 −14 Readme.md
  2. +6 −3 package.json
  3. +1 −1 smtp/client.js
  4. +16 −0 smtp/message.js
  5. +17 −4 smtp/smtp.js
  6. +1 −1 test/attachments/smtp.html
  7. +85 −0 test/authssl.js
  8. +0 −16 test/config.js.empty
  9. +249 −0 test/message.js
  10. +0 −137 test/run.js
  11. +0 −187 test/tests.js
View
@@ -1,4 +1,4 @@
-# emailjs (v0.2.8)
+# emailjs (v0.2.9)
send emails, html and attachments (files, streams and strings) from node.js to any smtp server
@@ -50,23 +50,19 @@ var server = email.server.connect({
ssl: true
});
-var headers = {
+var message = {
text: "i hope this works",
from: "you <username@gmail.com>",
to: "someone <someone@gmail.com>, another <another@gmail.com>",
cc: "else <else@gmail.com>",
- subject: "testing emailjs"
+ subject: "testing emailjs",
+ attachment:
+ [
+ {data:"<html>i <i>hope</i> this works!</html>", alternative:true},
+ {path:"path/to/file.zip", type:"application/zip", name:"renamed.zip"}
+ ]
};
-// create the message
-var message = email.message.create(headers);
-
-// attach an alternative html email for those with advanced email clients
-message.attach({data:"<html>i <i>hope</i> this works!</html>", alternative:true});
-
-// attach attachments because you can!
-message.attach({path:"path/to/file.zip", type:"application/zip", name:"renamed.zip"});
-
// send the message and get a callback with an error or details of the message that was sent
server.send(message, function(err, message) { console.log(err || message); });
@@ -101,7 +97,7 @@ server.send(message, function(err, message) { console.log(err || message); });
// callback will be executed with (err, message)
// either when message is sent or an error has occurred
-## email.message.create(headers)
+## message
// headers is an object ('from' and 'to' are required)
// returns a Message object
@@ -117,9 +113,10 @@ server.send(message, function(err, message) { console.log(err || message); });
cc // carbon copied recipients (same format as above)
bcc // blind carbon copied recipients (same format as above)
subject // string subject of the email
+ attachment // one attachment or array of attachments
}
-## Message.attach(options)
+## attachment
// can be called multiple times, each adding a new attachment
// options is an object with the following possible keys:
View
@@ -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.2.8",
+ "version": "0.2.9",
"author": "eleith",
"contributors":["izuzak", "Hiverness", "mscdex"],
"repository":
@@ -11,7 +11,10 @@
},
"devDependencies":
{
- "prompt": ">= 0.1.10"
+ "mocha": ">= 1.0.3",
+ "chai": ">= 1.0.3",
+ "simplesmtp" : ">= 0.1.18",
+ "mailparser" : ">= 0.2.26"
},
"dependencies":
{
@@ -21,6 +24,6 @@
"main": "email",
"scripts":
{
- "test": "node test/run.js"
+ "test": "mocha -R spec -t 5000 test/*.js"
}
}
View
@@ -119,7 +119,7 @@ Client.prototype =
{
// if we snag on SMTP commands, call done, passing the error
// but first reset SMTP state so queue can continue polling
- self.smtp.rset(function(err) { self._senddone(err, stack); });
+ self.smtp.rset(function() { self._senddone(err, stack); });
}
};
View
@@ -40,6 +40,22 @@ var Message = function(headers)
{
this.text = headers[header];
}
+ else if(header == "attachment" && typeof (headers[header]) == "object")
+ {
+ if((headers[header]).constructor == Array)
+ {
+ var that = this;
+
+ headers[header].forEach(function(attachment)
+ {
+ that.attach(attachment);
+ });
+ }
+ else
+ {
+ this.attach(headers[header]);
+ }
+ }
else
{
// allow any headers the user wants to set??
View
@@ -75,8 +75,8 @@ var SMTP = function(options)
this.monitor = null;
// keep these strings hidden when quicky debugging/logging
- this.user = function() { return options.user; }
- this.password = function() { return options.password; }
+ this.user = function() { return options.user; };
+ this.password = function() { return options.password; };
};
SMTP.prototype =
@@ -511,12 +511,25 @@ SMTP.prototype =
self.command((new Buffer(login.password())).toString("base64"), response, [235, 503]);
}
};
-
+
+ var attempt_user = function(err, data, msg)
+ {
+ if(err)
+ {
+ failed(err, data);
+ }
+ else
+ {
+ if(method == AUTH_METHODS.LOGIN)
+ self.command((new Buffer(login.user())).toString("base64"), attempt, [334]);
+ }
+ };
+
if(method == AUTH_METHODS.CRAM_MD5)
self.command("AUTH " + AUTH_METHODS.CRAM_MD5, attempt, [334]);
else if(method == AUTH_METHODS.LOGIN)
- self.command("AUTH " + AUTH_METHODS.LOGIN + " " + (new Buffer(login.user())).toString("base64"), attempt, [334]);
+ self.command("AUTH " + AUTH_METHODS.LOGIN, attempt_user, [334]);
else if(method == AUTH_METHODS.PLAIN)
self.command("AUTH " + AUTH_METHODS.PLAIN + " " + encode_plain(login.user(), login.password()), response, [235, 503]);
@@ -2,7 +2,7 @@
<html lang="en" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple Mail Transfer Protocol - Wikipedia, the free encyclopedia</title>
-<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="MediaWiki 1.18wmf1" />
<link rel="canonical" href="/wiki/Simple_Mail_Transfer_Protocol" />
View
@@ -0,0 +1,85 @@
+describe("authorize ssl", function()
+{
+ var simplesmtp = require("simplesmtp");
+ var expect = require("chai").expect;
+ var fs = require("fs");
+ var os = require("os");
+ var path = require('path');
+ var email = require('../email');
+ var port = 2526;
+ var server = null;
+ var smtp = null;
+
+ var send = function(message, verify)
+ {
+ smtp.on("startData", function(envelope)
+ {
+ envelope.parser = new (require("mailparser").MailParser)({defaultCharset:"utf-8"});
+ envelope.parser.on("end", function(mail)
+ {
+ verify(mail);
+ smtp.removeListener("startData", arguments.callee);
+ });
+ });
+
+ server.send(message, function(err)
+ {
+ if(err)
+ throw err;
+ });
+ }
+
+ before(function(done)
+ {
+ smtp = simplesmtp.createServer({secureConnection:true, requireAuthentication:true});
+
+ smtp.listen(port, function()
+ {
+ smtp.on("data", function(envelope, chunk)
+ {
+ envelope.parser.write(chunk);
+ });
+
+ smtp.on("dataReady", function(envelope, callback)
+ {
+ envelope.parser.end();
+ callback(null);
+ });
+
+ done();
+ });
+ });
+
+ after(function(done)
+ {
+ smtp.end(done);
+ });
+
+ it("login", function(done)
+ {
+ server = email.server.connect({port:port, user:"pooh", password:"honey", ssl:true});
+
+ var message =
+ {
+ subject: "this is a test TEXT message from emailjs",
+ from: "pooh@gmail.com",
+ to: "rabbit@gmail.com",
+ text: "hello friend, i hope this message finds you well."
+ };
+
+ smtp.on("authorizeUser", function(envelope, username, password, callback)
+ {
+ smtp.removeListener("authorizeUser", arguments.callee);
+ callback(null, username == "pooh" && password == "honey")
+ });
+
+ 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();
+ });
+ });
+});
View
@@ -1,16 +0,0 @@
-var config =
-{
- host: 'localhost',
- type: 'ssl', // tls or ssl, delete if you want neither
- // port: '25',
-
- username: 'username',
-
- // if your smtp requires authentication
- // password: null,
-
- email: null // where you want test emails going to
-};
-
-for(each in config)
- exports[each] = config[each];
Oops, something went wrong.

0 comments on commit 8ee6877

Please sign in to comment.