Permalink
Browse files

[api] Added templates back in. Implemented caching queue for template…

…s...should be working.
  • Loading branch information...
1 parent d121a9e commit fb8c11e2e023593c8a987671533d2f35b5068cb9 @Marak committed May 18, 2011
Showing with 78 additions and 14 deletions.
  1. +78 −14 lib/node_mailer.js
View
92 lib/node_mailer.js
@@ -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,94 @@ 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);
}
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: new Buffer(message.username || '', 'base64').toString('utf8'),
pass: new Buffer(message.password || '', 'base64').toString('utf8'),
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({
+ 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({
+ 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({
+ 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)
};

0 comments on commit fb8c11e

Please sign in to comment.