Sending Emails With Custom Data/Helpers


#1

Hi all,

just wondering if anyone has run into something similar:

Right now, I have an html template, which I run through juice and pass on down to SSR.compileTemplate and SSR.render, with custom data. You can see the basic outline below.

My question is this: once you start getting into large numbers of recipients, is there a better/more efficient way to handle this?

for(x=0;x<to.length;x++){
      if(Contacts.find({_id:to[x]}).count() !== 0){
        var recipient = Contacts.findOne({_id:to[x]});
        var emailData = {
          name: recipient.firstName + ' ' + recipient.lastName
        };
        to = recipient.email;

        Email.send({
          to: to,
          from: from,
          subject: subject,
          html: SSR.render( 'htmlEmail', emailData )
        });
      }
    }

#2

I would expect that SSR.render is quite readable stream so maybe just use some placeholder in template and than replace it in that cycle with some regexp ?
Or the good old shell way: render it and split by regexp and than in cycle
header part template
than insert name
footer part template


#3

Right, so maybe I should’ve explained a bit more – my template looks like this:

<template name="myEmail">
  <h1>Hi, {{name}}!</h1>
  <p>Email body</p>
</template>

Then I inline the styles with juice, and pass that to SSR.compileTemplate. I was more curious if there is an out-of-the-box method to handle multiple emails with dynamic recipients and data, or if my for loop is going to be a good way to handle that…


#4

Here’s a functional approach that might let mongo be more efficient (with bonus points for functional style):

db.collection.find({ _id : { $in : to}}).each((recipient) => {
....
}