2
votes

I'm developing a Node.js application that incorporates Sendgrid transaction email templates for things like user invitations. Specifically, I'm using nodejs v0.10.25 on Ubuntu w/ the official sendgrid v2 npm. My function to actually send messages is as follows:

function sendEmail(payload, options, cb){
    //for debugging purposes
    console.log(options);

    var email = new sendgrid.Email({
            subject: payload.subject,
            html: payload.html,
            from: global.config.sendgrid.mailFrom,
            fromname: global.config.sendgrid.mailName,
            replyto: options.replyto ? options.replyto : 'no-reply@' + global.config.sendgrid.mailhost
    });

    email.setSmtpapiTos(options.addressees);

    if(options.filters)
            email.setFilters(options.filters);

    if(options.subs)
            email.setSubstitutions(options.subs);

    if(options.category)
            email.setCategories(options.category);

    if(options.unique_args)
            email.setUniqueArgs(options.unique_args);

    sendgrid.send(email, function(err, json){
            return cb(err, json);
    });
}

The options object, which includes substitutions, looks like this:

{ category: 'Support Invite',
  addressees: [ %EMAIL1%, %EMAIL2% ],
  sub: 
   { '-name-': [ 'Shawn 1', 'Shawn 2' ],
     '-id-': [ 1, 2 ],
     '-pic-': 
      [ '<img height="100" src="%PICTURE URL%?sz=50" style="width: 100px; height: 100px;" width="100" />',
        '<img height="100" src="%PICTURE URL%?sz=50" style="width: 100px; height: 100px;" width="100" />' ] },
  filters: 
   { 
     templates: 
      { 
        settings:
         { 'enable': 1,
       'template_id': global.config.sendgrid.transactions.supportInvite} } },
  unique_args: 
   { 
     sender: '104294048950213400380' } }

Finally, here's the HTML for the template from this test:

<html>
<head>
	<title></title>
</head>
<body>
<div style="text-align: center;"><span>-pic-</span></div>

<div><span style="font-family:tahoma,geneva,sans-serif;">Hello -name-,</span></div>

<div>&nbsp;</div>

<div><span style="font-family:tahoma,geneva,sans-serif;">&lt;%body%&gt;</span></div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div><span style="font-family:tahoma,geneva,sans-serif;"><span style="font-size:9px;">Invitation ID:</span>&nbsp;-id-</span></div>
</body>
</html>

Messages are sent out to the appropriate recipients with correct template, subject, and sender information. I can also verify from my SendGrid stats that they're tagged with the appropriate category and SMTP argument ("sender"). But substitutions are not done. For example, the -name- tag still exists in the resulting email in my test email box as-is.

I've tried consulting documentation at SendGrid, including their SMTP API, and querying their support database. I've also searched StackExchange, but the most relevant questions (this, this, and this) don't quite offer an answer. Particularly interesting is that everyone uses different characters for substitution markers. Don't know if that's purely by choice or language dependent. I have tried different characters (-, : and %), but same result.

1

1 Answers

1
votes

I see your options object has a property sub, not subs. option.subs will always be undefined and therefore false in

if (options.subs)

Either try changing that to

if (options.sub)

Or rename the property in options from sub to subs.