I want to send emails from my nodejs application using sendgrid. So far I've generated an API KEY and I've followed the instructions in the nodejs docs
I've npm installed sendgrid and created my environment variables.
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const helper = require('sendgrid').mail;
const fromEmail = new helper.Email('test@example.com');
const toEmail = new helper.Email('myemail@gmail.com');
const subject = 'Using SendGrid is awesome';
const content = new helper.Content('text/html', '<p>hello world hello world</p>');
const mail = new helper.Mail(fromEmail, subject, toEmail, content);
const sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
const request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request, function (error, response) {
if (error) {
console.log('Error response received');
}
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
});
const index = require('./routes/index');
const users = require('./routes/users');
const app = express();
whenever I npm start the application I don't get an error; I get a 202 http code and of course everything seems to be working; except the console.log(response.body) doesn't console.log anything.
{ server: 'nginx',
date: 'Thu, 04 May 2017 21:16:13 GMT',
'content-type': 'text/plain; charset=utf-8',
'content-length': '0',
connection: 'close',
'x-message-id': 'HAlDggSxTqimI1GSBFxRAQ',
'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' }
except the console.log(response.body) doesn't console.log anything.
And then when I go on the sendgrid website to verify that the email was sent from my application; I instead receive a message telling me that my email couldn't be found, even though the integration code has clearly ran without an error.
Can anyone who has used sendgrid with nodejs pls help me and tell me what I'm doing wrong or any fixes?
via Emmanuel Wayne
No comments:
Post a Comment