Sebastian Stant
Sebastian Stant

Reputation: 3

Parse.com SendGrid Cloud Module: Cannot send header param object

I am try to send an email through Parse.com's SendGrid cloud module with the header param in order to include some unique arguments about the email being sent.

var SendGrid = require("sendgrid");  
SendGrid.initialize(username, password);

SendGrid.sendEmail({
  to: request.params.to,
  from: request.params.from,
  subject: request.params.subject,
  html: request.params.html,
  headers: JSON.stringify({ "unique_args": { "newsletter": request.params.newsletter }})
}, {
  success: function(httpResponse) {
    response.success(httpResponse);
  },
  error: function(httpResponse) {
    response.error(httpResponse);
  }
});

This gets the error message: JSON in headers is valid but incompatible

When I don't Stringify the header, I receive this error: Uncaught Error: Can't form encode an Object

More information about SendGrid Mail API: https://sendgrid.com/docs/API_Reference/Web_API/mail.html

More information about Parse.com SendGrid Cloud Module: https://www.parse.com/docs/cloud_modules_guide#sendgrid

Thanks!!

Upvotes: 0

Views: 675

Answers (1)

eddiezane
eddiezane

Reputation: 916

The key for the header would actually be x-smtpapi. You also don't need to stringify the json for this case.

Full usage with your example is:

var SendGrid = require("sendgrid");  
SendGrid.initialize(username, password);

SendGrid.sendEmail({
  to: request.params.to,
  from: request.params.from,
  subject: request.params.subject,
  html: request.params.html,
  "x-smtpapi": { "unique_args": { "newsletter": request.params.newsletter }}
}, {
  success: function(httpResponse) {
    response.success(httpResponse);
  },
  error: function(httpResponse) {
    response.error(httpResponse);
  }
});

Docs for the lib are here. It is actually easier to create an email object and work with it through the provided methods.

Upvotes: 1

Related Questions