I receive the following error when using SendGrid. Even though i get an error the mail is still sent.
null { statusCode: 202, body: '', headers: { server: 'nginx', date: 'Wed, 05 Jul 2017 18:50:52 GMT', 'content-type': 'text/plain; charset=utf-8', 'content-length': '0', connection: 'close', 'x-message-id': 'EUkSULsjRku6bJTkTFsQ9w', 'x-frame-options': 'DENY', 'access-control-allow-origin': 'https://sendgrid.api-docs.io', 'access-control-allow-methods': 'POST', 'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl', 'access-control-max-age': '600', 'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html' } } _http_outgoing.js:357 throw new Error('Can\'t set headers after they are sent.');
Using the following code:
var helper = require('sendgrid').mail,
sg = require('sendgrid')('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');
var from_email = new helper.Email('my.mail');
var to_email = new helper.Email('my.mai');
var subject = 'Subject';
var content = new helper.Content('text/plain', 'test');
var mail = new helper.Mail(from_email, subject, to_email, content);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request, function(err, response) {
console.log(err, response);
if (!err) {
res.send({
message: 'An email has been sent to the provided email with further instructions.'
});
} else {
return res.status(400).send({
message: 'Failure sending email'
});
}
});
How can i fix this?