1
0
mirror of https://github.com/eleith/emailjs.git synced 2024-06-25 16:29:04 +00:00

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

This commit is contained in:
eleith 2012-05-28 02:59:31 -07:00
parent 13ecc24931
commit 8ee6877e76
11 changed files with 386 additions and 363 deletions

View File

@ -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 File

@ -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 File

@ -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 File

@ -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 File

@ -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]);

View File

@ -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" />

85
test/authssl.js Normal file
View File

@ -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 File

@ -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];

249
test/message.js Normal file
View File

@ -0,0 +1,249 @@
describe("messages", 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();
smtp.listen(port, function()
{
server = email.server.connect({port:port});
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("simple text message", function(done)
{
var message =
{
subject: "this is a test TEXT message from emailjs",
from: "zelda@gmail.com",
to: "gannon@gmail.com",
text: "hello friend, i hope this message finds you well."
};
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("html data", function(done)
{
var html = fs.readFileSync(path.join(__dirname, "attachments/smtp.html"), "utf-8");
var message =
{
subject: "this is a test TEXT+HTML+DATA message from emailjs",
from: "obama@gmail.com",
to: "mitt@gmail.com",
text: "hello friend if you are seeing this, you can not view html emails. it is attached inline.",
attachment: {data:html, alternative:true}
};
send(message, function(mail)
{
expect(mail.html).to.equal(html);
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 file", function(done)
{
var html = fs.readFileSync(path.join(__dirname, "attachments/smtp.html"), "utf-8");
var headers =
{
subject: "this is a test TEXT+HTML+FILE message from emailjs",
from: "thomas@gmail.com",
to: "nikolas@gmail.com",
text: "hello friend if you are seeing this, you can not view html emails. it is attached inline.",
attachment: {path:path.join(__dirname, "attachments/smtp.html"), alternative:true}
};
send(headers, function(mail)
{
expect(mail.html).to.equal(html);
expect(mail.text).to.equal(headers.text + "\n");
expect(mail.headers.subject).to.equal(headers.subject);
expect(mail.headers.from).to.equal(headers.from);
expect(mail.headers.to).to.equal(headers.to);
done();
});
});
it("html with image embed", function(done)
{
var html = fs.readFileSync(path.join(__dirname, "attachments/smtp2.html"), "utf-8");
var image = fs.readFileSync(path.join(__dirname, "attachments/smtp.gif"));
var headers =
{
subject: "this is a test TEXT+HTML+IMAGE message from emailjs",
from: "ninja@gmail.com",
to: "pirate@gmail.com",
text: "hello friend if you are seeing this, you can not view html emails. it is attached inline.",
attachment:
{
path: path.join(__dirname, "attachments/smtp2.html"),
alternative: true,
related:
[
{
path: path.join(__dirname, "attachments/smtp.gif"),
type: "image/gif",
name: "smtp-diagram.gif",
headers: {"Content-ID":"<smtp-diagram@local>"}
}
]
}
};
send(headers, function(mail)
{
expect(mail.attachments[0].content.toString("base64")).to.equal(image.toString("base64"));
expect(mail.html).to.equal(html);
expect(mail.text).to.equal(headers.text + "\n");
expect(mail.headers.subject).to.equal(headers.subject);
expect(mail.headers.from).to.equal(headers.from);
expect(mail.headers.to).to.equal(headers.to);
done();
});
});
it("attachment", function(done)
{
var pdf = fs.readFileSync(path.join(__dirname, "attachments/smtp.pdf"));
var headers =
{
subject: "this is a test TEXT+ATTACHMENT message from emailjs",
from: "washing@gmail.com",
to: "lincoln@gmail.com",
text: "hello friend, i hope this message and pdf finds you well.",
attachment:{path:path.join(__dirname, "attachments/smtp.pdf"), type:"application/pdf", name:"smtp-info.pdf"}
};
send(headers, function(mail)
{
expect(mail.attachments[0].content.toString("base64")).to.equal(pdf.toString("base64"));
expect(mail.text).to.equal(headers.text + "\n");
expect(mail.headers.subject).to.equal(headers.subject);
expect(mail.headers.from).to.equal(headers.from);
expect(mail.headers.to).to.equal(headers.to);
done();
});
});
it("attachments", function(done)
{
var pdf = fs.readFileSync(path.join(__dirname, "attachments/smtp.pdf"));
var tar = fs.readFileSync(path.join(__dirname, "attachments/postfix-2.8.7.tar.gz"));
var headers =
{
subject: "this is a test TEXT+2+ATTACHMENTS message from emailjs",
from: "sergey@gmail.com",
to: "jobs@gmail.com",
text: "hello friend, i hope this message and attachments finds you well.",
attachment:
[
{path:path.join(__dirname, "attachments/smtp.pdf"), type:"application/pdf", name:"smtp-info.pdf"},
{path:path.join(__dirname, "attachments/postfix-2.8.7.tar.gz"), type:"application/tar-gz", name:"postfix.source.2.8.7.tar.gz"}
]
};
send(headers, function(mail)
{
expect(mail.attachments[0].content.toString("base64")).to.equal(pdf.toString("base64"));
expect(mail.attachments[1].content.toString("base64")).to.equal(tar.toString("base64"));
expect(mail.text).to.equal(headers.text + "\n");
expect(mail.headers.subject).to.equal(headers.subject);
expect(mail.headers.from).to.equal(headers.from);
expect(mail.headers.to).to.equal(headers.to);
done();
});
});
it("streams", function(done)
{
var pdf = fs.readFileSync(path.join(__dirname, "attachments/smtp.pdf"));
var tar = fs.readFileSync(path.join(__dirname, "attachments/postfix-2.8.7.tar.gz"));
var stream = fs.createReadStream(path.join(__dirname, "attachments/smtp.pdf"));
var stream2 = fs.createReadStream(path.join(__dirname, "attachments/postfix-2.8.7.tar.gz"));
var headers =
{
subject: "this is a test TEXT+2+STREAMED+ATTACHMENTS message from emailjs",
from: "stanford@gmail.com",
to: "mit@gmail.com",
text: "hello friend, i hope this message and streamed attachments finds you well.",
attachment:
[
{stream:stream, type:"application/pdf", name:"smtp-info.pdf"},
{stream:stream2, type:"application/x-gzip", name:"postfix.source.2.8.7.tar.gz"}
]
};
stream.pause();
stream2.pause();
send(headers, function(mail)
{
expect(mail.attachments[0].content.toString("base64")).to.equal(pdf.toString("base64"));
expect(mail.attachments[1].content.toString("base64")).to.equal(tar.toString("base64"));
expect(mail.text).to.equal(headers.text + "\n");
expect(mail.headers.subject).to.equal(headers.subject);
expect(mail.headers.from).to.equal(headers.from);
expect(mail.headers.to).to.equal(headers.to);
done();
});
});
});

View File

@ -1,137 +0,0 @@
var prompt = require('prompt');
var os = require('os');
var path = require('path');
var tests = require('./tests');
var email = require('../email');
var run =
{
tests: function(config)
{
var server = email.server.connect(
{
user: config.username,
password: config.password,
host: config.host,
port: config.port,
tls: config.type == 'tls',
ssl: config.type == 'ssl'
});
for(var test in tests)
{
tests[test](email, server, config);
}
},
prompts: function(config)
{
var prompts =
{
host:
{
name: 'host',
message: 'smtp hostname or IP',
'default': 'localhost'
},
type:
{
name: 'type',
message: 'ssl, tls or none',
validator: /^(ssl|tls|none)$/,
warning: "connection type must be 'ssl', 'tls', 'none'",
'default': 'tls'
},
username:
{
name: 'username',
message: "smtp username",
'default': ''
},
password:
{
name: 'password',
message: "smtp password",
hidden: true,
'default': ''
}
};
var ask = [];
for(var each in prompts)
{
if(!config[each])
ask.push(prompts[each]);
}
if(ask.length)
{
prompt.start();
prompt.message = "";
prompt.delimiter = ">";
prompt.addProperties(config, ask, function(err) { run.prompts2(config); });
}
else
run.prompts2(config);
},
prompts2: function(config)
{
var ask = [];
if(!config.port)
{
ask.push(
{
name: 'port',
message: 'smtp port',
'default': config.type == 'tls' ? 587 : config.type == 'ssl' ? 465 : 25
});
}
if(!config.email)
{
ask.push(
{
name: 'email',
message: 'your email address',
empty: false,
validator: /.+@.+/,
warning: 'not a valid email address'
});
}
if(ask.length)
{
if(!prompt.started)
prompt.start();
prompt.message = "";
prompt.delimiter = ">";
prompt.addProperties(config, ask, function(err)
{
process.stdin.destroy();
run.tests(config);
});
}
else
{
process.stdin.destroy();
run.tests(config);
}
}
};
path.exists(path.join(__dirname, 'config.js'), function(exists)
{
var config = {};
if(exists)
config = require(path.join(__dirname, 'config.js'));
run.prompts(config);
});

View File

@ -1,187 +0,0 @@
var tests =
{
text: function(email, server, config)
{
var message = email.message.create(
{
subject: "this is a test TEXT message from emailjs",
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
to: config.email,
text: "hello friend, i hope this message finds you well."
});
server.send(message, function(err, message)
{
console.log(err ? err.message : "text only email successfully sent");
});
},
html_data: function(email, server, config)
{
var path = require('path');
var fs = require('fs');
var message = email.message.create(
{
subject: "this is a test TEXT+HTML+DATA message from emailjs",
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
to: config.email,
text: "hello friend if you are seeing this, you can not view html emails. it is attached inline."
});
// attach an alternative html email for those with advanced email clients
message.attach({data:fs.readFileSync(path.join(__dirname, "attachments/smtp.html"), "utf-8"), alternative:true});
server.send(message, function(err, message)
{
console.log(err ? err.message : "text+html+data email successfully sent");
});
},
html_file: function(email, server, config)
{
var path = require('path');
var fs = require('fs');
var message = email.message.create(
{
subject: "this is a test TEXT+HTML+FILE message from emailjs",
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
to: config.email,
text: "hello friend if you are seeing this, you can not view html emails. it is attached inline."
});
// attach an alternative html email for those with advanced email clients
message.attach({path:path.join(__dirname, "attachments/smtp.html"), alternative:true});
server.send(message, function(err, message)
{
console.log(err ? err.message : "text+html+file email successfully sent");
});
},
html_embedded_image: function(email, server, config)
{
var path = require('path');
var fs = require('fs');
var message = email.message.create(
{
subject: "this is a test TEXT+HTML+IMAGE message from emailjs",
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
to: config.email,
text: "hello friend if you are seeing this, you can not view html emails. it is attached inline."
});
// attach an alternative html email for those with advanced email clients
message.attach(
{
path: path.join(__dirname, "attachments/smtp2.html"),
alternative: true,
related:
[
{
path: path.join(__dirname, "attachments/smtp.gif"),
type: "image/gif",
name: "smtp-diagram.gif",
headers: {"Content-ID":"<smtp-diagram@local>"}
}
]
});
//message.attach({path:path.join(__dirname, "attachments/smtp.gif"), type:"image/gif", name:"smtp-diagram.gif", headers:{"Content-ID":"<smtp-diagram@local>"}});
server.send(message, function(err, message)
{
console.log(err ? err.message : "text+html+image email successfully sent");
});
},
attachment: function(email, server, config)
{
var path = require('path');
var message = email.message.create(
{
subject: "this is a test TEXT+ATTACHMENT message from emailjs",
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
to: config.email,
text: "hello friend, i hope this message and pdf finds you well."
});
message.attach({path:path.join(__dirname, "attachments/smtp.pdf"), type:"application/pdf", name:"smtp-info.pdf"});
server.send(message, function(err, message)
{
console.log(err ? err.message : "text+attachment email successfully sent");
});
},
attachments: function(email, server, config)
{
var path = require('path');
var fs = require('fs');
var message = email.message.create(
{
subject: "this is a test TEXT+2+ATTACHMENTS message from emailjs",
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
to: config.email,
text: "hello friend, i hope this message and attachments finds you well."
});
message.attach({path:path.join(__dirname, "attachments/smtp.pdf"), type:"application/pdf", name:"smtp-info.pdf"});
message.attach({path:path.join(__dirname, "attachments/postfix-2.8.7.tar.gz"), type:"application/tar-gz", name:"postfix.source.2.8.7.tar.gz"});
server.send(message, function(err, message)
{
console.log(err ? err.message : "text+2+attachments email successfully sent");
});
},
streams: function(email, server, config)
{
var path = require('path');
var fs = require('fs');
var message = email.message.create(
{
subject: "this is a test TEXT+2+STREAMED+ATTACHMENTS message from emailjs",
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
to: config.email,
text: "hello friend, i hope this message and streamed attachments finds you well."
});
var stream = fs.createReadStream(path.join(__dirname, "attachments/smtp.pdf"));
stream.pause();
message.attach({stream:stream, type:"application/pdf", name:"smtp-info.pdf"});
var stream2 = fs.createReadStream(path.join(__dirname, "attachments/postfix-2.8.7.tar.gz"));
stream2.pause();
message.attach({stream:stream2, type:"application/x-gzip", name:"postfix.source.2.8.7.tar.gz"});
server.send(message, function(err, message)
{
console.log(err ? err.message : "text+2+streamed+attachments email successfully sent");
});
},
legacy_apis: function(email, server, config)
{
var path = require('path');
var fs = require('fs');
var message = email.message.create(
{
subject: "this is a test of legacy API ATTACHMENTS message from emailjs",
from: /^.+@.+$/.test(config.username) ? config.username : config.username + '@' + config.host,
to: config.email,
text: "hello friend, i hope this message and html and attachments finds you well."
});
// attach an alternative html email for those with advanced email clients
message.attach_alternative(fs.readFileSync(path.join(__dirname, "attachments/smtp.html"), "utf-8"));
message.attach(path.join(__dirname, "attachments/smtp.pdf"), "application/pdf", "smtp-info.pdf");
server.send(message, function(err, message)
{
console.log(err ? err.message : "text+legacy+attachments email successfully sent");
});
}
};
for(var test in tests)
exports[test] = tests[test];