2

(Open to suggestion on the lingo up there)

I'm trying to set up a simple email sender from a node console app. After reading this answer node-email-templates seems like the way to go. But I can't figure out how to get this library to work in an "initialize now, use later" style object. The example code doesn't do that, it's initializing the object and sending at the same time. Node-mailer gives a good example of the style I'm used to:

var nodemailer = require("nodemailer");

// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "gmail.user@gmail.com",
        pass: "userpass"
    }
});

// setup e-mail data with unicode symbols
var mailOptions = {
    from: "Fred Foo ✔ <foo@blurdybloop.com>", // sender address
    to: "bar@blurdybloop.com, baz@blurdybloop.com", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
}

// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    // if you don't want to use this transport object anymore,
    // uncomment following line:
    // smtpTransport.close(); // shut down the connection pool, no more messages
});

What I'd really like to be able to do:

// require node-email-templates 

// setup smtp transport, store it as a property of another object,
//   let's call it Alert 

// when an alert needs to send an email it will pick a template 
//   name and tell node-email-templates to send that template, 
//   with appropriate local variables 

This doesn't seem unusual in any way but damned if I can get it to work based on the sample code that is provided.

Community
  • 1
  • 1
jcollum
  • 43,623
  • 55
  • 191
  • 321

1 Answers1

0

After looking at the source and discovering that the main entry of node-email-templates is structured as an event-driven process, I doubt you can do what you need without substantial effort.

If that doesn't deter you, you might consider extracting things like the creation of the renderer and transport from the module's main.js function entry point and refactoring it to include more arguments representing the pre-configured resources you need.

Another possible approach might be to add calls to externally defined event-handlers in the main entry function to set references to your external resources.

It should be noted that both of these changes will limit your ability to update the existing module without losing your modifications.

But as the library was written, there is no easy way to convert what is fundamentally an event-driven process into a linear, procedural one without modifying its source.

Rob Raisch
  • 17,040
  • 4
  • 48
  • 58
  • I suspect you're right. I'll mark this as the answer in a while; want to see if anyone else has an answer. – jcollum Jul 02 '14 at 15:57