Permalink
Browse files

[api][fix] expose the entire NodeMailer options.

  • Loading branch information...
2 parents d06ed15 + e76f156 commit b9a658001a8ddd973e3c3ebcb6f1feeac9e7bcf1 @bmeck bmeck committed May 27, 2011
Showing with 97 additions and 19 deletions.
  1. +4 −2 Readme.md
  2. +1 −1 demo-template.js
  3. +2 −1 demo.js
  4. +89 −14 lib/node_mailer.js
  5. +1 −1 package.json
View
@@ -1,4 +1,4 @@
-#v0.4.51
+#v0.6.0
<img src = "https://github.com/Marak/node_mailer/raw/master/logo.png"/><br/>
###send emails from node.js to your smtp server, simple as cake.
@@ -51,7 +51,7 @@ add an option ssl: true. See usage above.
### create a simple template
-/templates/sample.text
+/templates/sample.txt
Hello {{username}},
@@ -97,6 +97,8 @@ add an option ssl: true. See usage above.
+
+
## Authors
Marak Squires, Elijah Insua, Fedor Indutny, Bradley Meck
View
@@ -9,7 +9,7 @@ for(var i = 0; i < 10; i++){
to : "marak.squires@gmail.com",
from : "obama@whitehouse.gov",
subject : "node_mailer test email",
- template : "../templates/sample.txt", // path to template name
+ template : "./templates/sample.txt", // path to template name
data : {
"username": "Billy Bob",
"color": function(){
View
@@ -1,6 +1,6 @@
var email = require("./lib/node_mailer");
-for(var i = 0; i < 10; i++){
+for(var i = 0; i < 1; i++){
email.send({
ssl: true,
host : "smtp.gmail.com", // smtp server hostname
@@ -9,6 +9,7 @@ for(var i = 0; i < 10; i++){
to : "bradley.meck@gmail.com",
from : "bradley.meck@gmail.com",
subject : "node_mailer test email",
+ reply_to:"bradley@bradleymeck.com",
body: "Hello! This is a test of the node_mailer.",
authentication : "login", // auth login is supported; anything else is no auth
username : undefined, // username
View
@@ -24,6 +24,9 @@ OTHER DEALINGS IN THE SOFTWARE.
var SMTPClient = require("nodemailer").SMTPClient;
var EmailMessage = require("nodemailer").EmailMessage;
+var fs = require('fs');
+var mustache = require('../vendor/mustache');
+var _templateCache = {};
function SMTPClientPool() {
this.servers = {};
@@ -46,33 +49,105 @@ SMTPClientPool.prototype.addClient = function(port,host,options) {
delete hostClients[options.user];
client.close();})
}
-SMTPClientPool.prototype.send = function send(message,callback) {
+SMTPClientPool.prototype.send = function send(message, callback) {
this.servers[message.SERVER.host][message.SERVER.user].sendMail(message,callback);
}
+function merge(x,y) {
+ var z = {};
+ for(var k in x) {
+ z[k] = x[k];
+ }
+ for(var k in y) {
+ z[k] = y[k];
+ }
+ return z;
+}
+
var pool = new SMTPClientPool();
-exports.send = function node_mail(message,callback) {
+exports.send = function node_mail(message, callback) {
+
var server = {
host: message.host,
hostname: message.domain,
- port: +message.port,
+ port: + message.port,
use_authentication: message.authentication,
ssl: message.ssl,
user: message.username && message.username.toString(),
pass: message.password && message.password.toString(),
debug: true
- }
+ };
+
if(message.username || message.password) {
- pool.addClient(server.port,server.host,server);
+ pool.addClient(server.port, server.host, server);
+ }
+
+ // If a template was passed in as part of the message
+ if (message.template) {
+ // If the template path is in the cache
+ if (_templateCache[message.template]) {
+ // If the template is already fully loaded in the cahe
+ if (_templateCache[message.template].loaded) {
+ // Use the cached template and send the email
+ message.body = mustache.to_html(_templateCache[message.template].template, message.data);
+ pool.send(new EmailMessage(merge(message,{
+ to: message.to,
+ sender: message.from,
+ subject: message.subject,
+ body: message.body,
+ server: server,
+ debug: message.debug
+ })), callback);
+ }
+ else {
+ // We've started to load the template, but it's not loaded yet. queue up this message to be sent later
+ _templateCache[message.template].queue.push(message);
+ }
+ }
+ else {
+ // The template path wasn't found in the cache, start to load the template
+ _templateCache[message.template] = {};
+ _templateCache[message.template].loaded = false;
+ _templateCache[message.template].template = '';
+ _templateCache[message.template].queue = [];
+
+ fs.readFile(message.template, function(err, result){
+ if (err) {
+ console.log(err);
+ return;
+ }
+
+ _templateCache[message.template].template = result.toString();
+ _templateCache[message.template].loaded = true;
+
+ // "Drain" the queue
+ _templateCache[message.template].queue.forEach(function(msg, i){
+ msg.body = mustache.to_html(_templateCache[message.template].template, msg.data);
+ pool.send(new EmailMessage(merge(msg,{
+ to: msg.to,
+ sender: msg.from,
+ subject: msg.subject,
+ body: msg.body,
+ server: server,
+ debug: msg.debug
+ })), callback);
+ });
+
+ // Clear the queue out
+ _templateCache[message.template].queue = [];
+
+ });
+ }
+ }
+ else { // No template being used
+ pool.send(new EmailMessage(merge(message,{
+ to: message.to,
+ sender: message.from,
+ subject: message.subject,
+ body: message.body,
+ server: server,
+ debug: message.debug
+ })), callback);
}
- pool.send(new EmailMessage({
- to: message.to,
- sender: message.from,
- subject: message.subject,
-
- body: message.body,
- server: server,
- debug: message.debug
- }),callback)
};
View
@@ -1,7 +1,7 @@
{
"name": "mailer",
"description": "send emails from node.js to a smtp server, simple as cake",
- "version": "0.5.52",
+ "version": "0.6.0",
"author": "Marak Squires",
"contributors" : [
"Elijah Insua <tmpvar@gmail.com> (http://tmvpar.com/)",

0 comments on commit b9a6580

Please sign in to comment.