Sendgrid-nodejs: Cannot use Substitutions neither in Personalizations nor in Mail objects

Created on 29 Aug 2017  Â·  9Comments  Â·  Source: sendgrid/sendgrid-nodejs

Issue Summary

// Setup sendgrid api
const sendGridMail = require('@sendGrid/mail');
sendGridMail.setApiKey('<API_key goes here>');

//build object 
var mailOptions = {
    personalizations:[{
      to: to,
      substitutions: {'first_name':'John'}
    }],
    from: '[email protected]',
    reply_to: '[email protected]',
    subject: 'Hello',
    html: 'email text goes here',
    templateId: 'aa6733f7-0bbb-4a76-a022-588f04e10ca4'
  };

//send 
return sendGridMail.send(mailOptions);

I'm receiving the following error:

There was an error while sending the email: TypeError: personalization.reverseMergeSubstitutions is not a function
    at Mail.applySubstitutions (/user_code/node_modules/@sendgrid/helpers/classes/mail.js:278:21)
    at Mail.addPersonalization (/user_code/node_modules/@sendgrid/helpers/classes/mail.js:228:10)
    at personalizations.forEach.personalization (/user_code/node_modules/@sendgrid/helpers/classes/mail.js:221:40)
    at Array.forEach (native)
    at Mail.setPersonalizations (/user_code/node_modules/@sendgrid/helpers/classes/mail.js:221:8)
    at Mail.fromData (/user_code/node_modules/@sendgrid/helpers/classes/mail.js:95:12)
    at new Mail (/user_code/node_modules/@sendgrid/helpers/classes/mail.js:41:12)
    at Function.create (/user_code/node_modules/@sendgrid/helpers/classes/mail.js:555:12)
    at MailService.send (/user_code/index.js:489:25)
    at Promise.all.then (/user_code/index.js:387:29)

If don't use personalizations (and use Substitutions in the root object), then email is sent fine, but Substitutions are not replaced, because @sendgrid/helpers/classes/Mail object in its toJSON method ignores 'substitutions' property.

Technical details:

  • sendgrid-nodejs Version: 6.0.0
  • Node.js Version: 6.11
help wanted question

All 9 comments

Hi @dbaranovsky,

I was able to reproduce and I've added this to our backlog for a fix.

For now, this example works.

Here is the code I used to test:

// Setup sendgrid api
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgMail.setSubstitutionWrappers('-', '-'); // Configure the substitution tag wrappers globally

const msg = {
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: '<p>Hello HTML world!</p>',
  templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
  substitutions: {
    name: 'Some One',
    city: 'Denver',
  },
};
sgMail.send(msg);

Thanks!

With Best Regards,

Elmer

@adamreisnz,

Thoughts on this one?

I'll have a look!

PR created to fix this issue

Having this issue on all subs:

trying to sub a form response template with the following:

{ "email": "[email protected]", "to": "[email protected]", "from":"[email protected]", "subject":"Thank You For Contacting Us", "substitutions" : { "first_name": "John", "last_name": "Doe", "contact_method": "Email", "substitutionWrappers": ["{{", "}}"], "email_user": "[email protected]", "message_user": "The Message....." }, "templateId": "TheID....." }

Into:

Your Full Name
{{first_name}} {{last_name}}

Your Email
{{email_user}}

We May Contact You By
{{contact_method}}

Your Message
{{message_user}}

Which should yield :

YourFullName
John Doe

Your Email
[email protected]

We May Contact You By
Email

Your Message
The Message.....

Instead, i get:

YourFullName
John {{last_name}}

Your Email
{{email_user}}

We May Contact You By
{{contact_method}}

Your Message
The Message....

Using the latest Node Mail SDK. This only works with the .send method, since using the POST method with postman works fine...
Any insights would be great at this point. so far I've tried applying the substitutionWrappers field, and tried not applying it.. yields the same result.

Cheers

Could you please post the entire code you're using to send & construct the email? I can see your configuration and at first glance it looks ok, but I'd like to see the full JS.

Are you using @sendgrid/mail and not by any chance the old sendgrid package?

I'm using sendgrid/mail

Turns out that in my test I had a top-level "email" field that I'd forgotten to remove.

as in

{"email":"[email protected]", ...the rest of the object...}

removing the field fixed the issue and things worked normally. however, re-implementing it still produced the issue. So it's a fix on my end, however just letting you guys know this does happen.

The code used is basically your example in your Readme for template usage, whereas:

// Setup sendgrid api
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgMail.setSubstitutionWrappers('-', '-'); // Configure the substitution tag wrappers globally

const msg = {
  email: '[email protected]',
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: '<p>Hello HTML world!</p>',
  templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
  substitutions: {
    name: 'Some One',
    city: 'Denver',
  },
};
sgMail.send(msg);

Produces the error and this:

// Setup sendgrid api
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgMail.setSubstitutionWrappers('-', '-'); // Configure the substitution tag wrappers globally

const msg = {
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: '<p>Hello HTML world!</p>',
  templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
  substitutions: {
    name: 'Some One',
    city: 'Denver',
  },
};
sgMail.send(msg);

Does not.

Thanks for the reply either way!

Right, email shouldn't be there!

On Wed, Feb 14, 2018, 06:26 murphman300 notifications@github.com wrote:

I'm using sendgrid/mail

Turns out that in my test I had a top-level "email" field that I'd
forgotten to remove.

as in

{"email":"[email protected]", ...the rest of the object...}

removing the field fixed the issue and things worked normally. however,
re-implementing it still produced the issue. So it's a fix on my end,
however just letting you guys know this does happen.

The code used is basically your example in your Readme for template usage,
whereas:

// Setup sendgrid api
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgMail.setSubstitutionWrappers('-', '-'); // Configure the substitution tag wrappers globally

const msg = {
email: '[email protected]',
to: 'elmer.[email protected]',
from: '[email protected]',
subject: 'Hello world',
text: 'Hello plain world!',
html: '

Hello HTML world!

',
templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
substitutions: {
name: 'Some One',
city: 'Denver',
},
};
sgMail.send(msg);

Produces the error and this:

// Setup sendgrid api
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgMail.setSubstitutionWrappers('-', '-'); // Configure the substitution tag wrappers globally

const msg = {
to: 'elmer.[email protected]',
from: '[email protected]',
subject: 'Hello world',
text: 'Hello plain world!',
html: '

Hello HTML world!

',
templateId: '13b8f94f-bcae-4ec6-b752-70d6cb59f932',
substitutions: {
name: 'Some One',
city: 'Denver',
},
};
sgMail.send(msg);

Does not.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/sendgrid/sendgrid-nodejs/issues/433#issuecomment-365340395,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAd8Qgev1QXQvqueU4uLZBxMahM37-a3ks5tUcXPgaJpZM4PGk_I
.

Thanks @murphman300 for the follow up and @adamreisnz for the confirmation!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amlcodes picture amlcodes  Â·  4Comments

Loriot-n picture Loriot-n  Â·  4Comments

polkhovsky picture polkhovsky  Â·  3Comments

prasoonjalan picture prasoonjalan  Â·  3Comments

umarhussain15 picture umarhussain15  Â·  3Comments