Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use Substitutions neither in Personalizations nor in Mail objects #433

Closed
dbaranovsky opened this issue Aug 29, 2017 · 9 comments · Fixed by #436
Closed

Cannot use Substitutions neither in Personalizations nor in Mail objects #433

dbaranovsky opened this issue Aug 29, 2017 · 9 comments · Fixed by #436
Labels
status: help wanted requesting help from the community type: question question directed at the library

Comments

@dbaranovsky
Copy link

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: 'info@fordemo.io',
    reply_to: 'support@fordemo.io',
    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
@thinkingserious thinkingserious added status: help wanted requesting help from the community type: question question directed at the library labels Aug 29, 2017
@thinkingserious
Copy link
Contributor

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: 'elmer.thomas@sendgrid.com',
  from: 'dx@sendgrid.com',
  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

@thinkingserious
Copy link
Contributor

@adamreisnz,

Thoughts on this one?

@adamreisnz
Copy link
Contributor

I'll have a look!

@adamreisnz
Copy link
Contributor

PR created to fix this issue

@murphman300
Copy link

murphman300 commented Feb 13, 2018

Having this issue on all subs:

trying to sub a form response template with the following:

{ "email": "jeanlouis@spotit.ca", "to": "some@address.ca", "from":"noreply@address.ca", "subject":"Thank You For Contacting Us", "substitutions" : { "first_name": "John", "last_name": "Doe", "contact_method": "Email", "substitutionWrappers": ["{{", "}}"], "email_user": "some@address.ca", "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
some@address.ca

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

@adamreisnz
Copy link
Contributor

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?

@murphman300
Copy link

murphman300 commented Feb 13, 2018

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":"some@email.com", ...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: 'some@email.com',
  to: 'elmer.thomas@sendgrid.com',
  from: 'dx@sendgrid.com',
  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: 'elmer.thomas@sendgrid.com',
  from: 'dx@sendgrid.com',
  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!

@adamreisnz
Copy link
Contributor

adamreisnz commented Feb 13, 2018 via email

@thinkingserious
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: question question directed at the library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants