Browse files

Merge pull request #49 from shripadk/master

[fix] HTML template issue.
  • Loading branch information...
2 parents 4edf448 + 5990363 commit f8f18d42bc6f1f45fbdfcd380f05fcbf3650ef46 @Marak committed Oct 10, 2011
Showing with 61 additions and 32 deletions.
  1. +32 −0 demo-template-html.js
  2. +2 −1 demo-template.js
  3. +2 −1 demo.js
  4. +17 −29 lib/node_mailer.js
  5. +1 −1 package.json
  6. +7 −0 templates/sample-html.txt
View
32 demo-template-html.js
@@ -0,0 +1,32 @@
+var email = require("./lib/node_mailer");
+
+for(var i = 0; i < 10; i++){
+
+ email.send({
+ host : "localhost", // smtp server hostname
+ port : "25", // smtp server port
+ domain : "localhost", // domain used by client to identify itself to server
+ to : "marak.squires@gmail.com",
+ from : "obama@whitehouse.gov",
+ subject : "node_mailer test email",
+ template : "./templates/sample-html.txt", // path to template name
+ data : {
+ "username": "Billy Bob",
+ "color": function(){
+ var arr = ["purple", "red", "green", "yello"];
+ return arr[Math.floor(Math.random()*3)];
+ },
+ "animal": "monkey",
+ "adverb": "quickly",
+ "noun": "hot lava"
+ },
+
+ authentication : "login", // auth login is supported; anything else is no auth
+ username : undefined, // username
+ password : undefined, // password
+ debug: true // log level per message
+ },
+ function(err, result){
+ if(err){ console.log(err); }
+ });
+}
View
3 demo-template.js
@@ -23,7 +23,8 @@ for(var i = 0; i < 10; i++){
authentication : "login", // auth login is supported; anything else is no auth
username : undefined, // username
- password : undefined // password
+ password : undefined, // password
+ debug: true // log level per message
},
function(err, result){
if(err){ console.log(err); }
View
3 demo.js
@@ -13,7 +13,8 @@ for(var i = 0; i < 1; i++){
body: "Hello! This is a test of the node_mailer.",
authentication : "login", // auth login is supported; anything else is no auth
username : undefined, // username
- password : undefined // password
+ password : undefined, // password
+ debug: true // log level per message
},
function(err, result){
if(err){ console.log(err); }
View
46 lib/node_mailer.js
@@ -94,29 +94,33 @@ exports.send = function node_mail(message, callback) {
ssl: message.ssl,
user: message.username && message.username.toString(),
pass: message.password && message.password.toString(),
- debug: true
+ debug: message.debug || false
};
if(message.username || message.password) {
pool.addClient(server.port, server.host, server);
}
+ function dispatchMail(message, server, callback) {
+ var _message = {
+ to: message.to,
+ sender: message.from,
+ subject: message.subject,
+ server: server,
+ debug: message.debug
+ };
+ if(message.html)_message.html = message.html;
+ pool.send(new EmailMessage(merge(message, _message)), callback);
+ }
+
// 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,
- html: message.html,
- server: server,
- debug: message.debug
- })), callback);
+ message.html = mustache.to_html(_templateCache[message.template].template, message.data);
+ dispatchMail(message, server, callback);
}
else {
// We've started to load the template, but it's not loaded yet. queue up this message to be sent later
@@ -143,31 +147,15 @@ exports.send = function node_mail(message, callback) {
_templateCache[message.template].queue.push(message);
_templateCache[message.template].queue.forEach(function(msg, i){
msg.html = mustache.to_html(_templateCache[message.template].template, msg.data);
- pool.send(new EmailMessage(merge(msg,{
- to: msg.to,
- sender: msg.from,
- subject: msg.subject,
- html: msg.html,
- server: server,
- debug: msg.debug
- })), callback);
+ dispatchMail(msg, server, 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,
- html: message.html,
- server: server,
- debug: message.debug
- })), callback);
+ dispatchMail(message, server, callback);
}
};
View
2 package.json
@@ -13,7 +13,7 @@
"url": "http://github.com/Marak/node_mailer.git"
},
"dependencies": {
- "nodemailer": ">= 0.1.10",
+ "nodemailer": "0.1.20",
"colors": ">= 0.3.0"
},
"engine": [ "node >=0.3.0" ],
View
7 templates/sample-html.txt
@@ -0,0 +1,7 @@
+<div>Hello {{username}}, </div>
+<br/>
+<div>This is a sample template of the node mailer.</div>
+<br/>
+<div>It uses mustache templating to do basic search and replaces. </div>
+<br/>
+<div>The {{color}} {{animal}} {{adverb}} ran over the {{noun}}.</div>

0 comments on commit f8f18d4

Please sign in to comment.