Skip to content
This repository

Responding with the correct callback #33

Open
wants to merge 4 commits into from

2 participants

tanguylebarzic
tanguylebarzic

When the messages are queued, the callbacks called once they are sent should be the ones given as argument for each email, not the one of the last email.

tanguylebarzic

(I agree it seldom happens, just in case).

Regards,

Tanguy

Owner
Marak commented

I think this makes a lot of sense. Let me do a sanity check for review and we'll merge.

Thank you!

tanguylebarzic

Hi,

I added another small fix : if a file was missing, the first time the method was called the callback would be called with the proper error, but the next times, it would never be called.

Another way of fixing it would be to set _templateCache[message.template].error to the error raised, and to respond this for each new call to the method (ie. without having to call readFile each time), but it would mean the server has to be reloaded when the missing file is eventually added, which I didn't like personally.

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 7 additions and 3 deletions. Show diff stats Hide diff stats

  1. +7 3 lib/node_mailer.js
10 lib/node_mailer.js
@@ -119,7 +119,7 @@ exports.send = function node_mail(message, callback) {
119 119 }
120 120 else {
121 121 // We've started to load the template, but it's not loaded yet. queue up this message to be sent later
122   - _templateCache[message.template].queue.push(message);
  122 + _templateCache[message.template].queue.push({message: message, callback: callback});
123 123 }
124 124 }
125 125 else {
@@ -131,6 +131,7 @@ exports.send = function node_mail(message, callback) {
131 131
132 132 fs.readFile(message.template, function(err, result){
133 133 if (err) {
  134 + _templateCache[message.template] = false;
134 135 callback(err);
135 136 return;
136 137 }
@@ -139,8 +140,11 @@ exports.send = function node_mail(message, callback) {
139 140 _templateCache[message.template].loaded = true;
140 141
141 142 // "Drain" the queue
142   - _templateCache[message.template].queue.push(message);
143   - _templateCache[message.template].queue.forEach(function(msg, i){
  143 + _templateCache[message.template].queue.push({message: message, callback: callback});
  144 + _templateCache[message.template].queue.forEach(function(queuedItem, i){
  145 + var msg = queuedItem.message;
  146 + var callback = queuedItem.callback;
  147 +
144 148 msg.html = mustache.to_html(_templateCache[message.template].template, msg.data);
145 149 pool.send(new EmailMessage(merge(msg,{
146 150 to: msg.to,

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.