nodemailer / nodemailer-sendgrid

SendGrid transport for Nodemailer
46 stars 18 forks source link

Getting unauthorised - 401 error when sending email. #5

Closed itsyogesh closed 6 years ago

itsyogesh commented 6 years ago

Hey everyone,

I am getting an unauthorised error when I am using this library to send an email. Here is the error.

{ Error: Unauthorized
    at Request.http [as _callback] (node_modules/@sendgrid/client/src/classes/client.js:124:25)
    at Request.self.callback (node_modules/request/request.js:185:22)
    at Request.emit (events.js:182:13)
    at Request.<anonymous> (node_modules/request/request.js:1157:10)
    at Request.emit (events.js:182:13)
    at IncomingMessage.<anonymous> (node_modules/request/request.js:1079:12)
    at Object.onceWrapper (events.js:273:13)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1081:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  code: 401,
  message: 'Unauthorized',
  response:
   { headers:
      { server: 'nginx',
        date: 'Tue, 31 Jul 2018 13:15:41 GMT',
        'content-type': 'application/json',
        'content-length': '88',
        connection: 'close',
        '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' },
     body: undefined } }

I have followed the example in this repo and my sendgrid transport looks like this:

const nodemailer = require('nodemailer')
const nodemailerSendgrid = require('nodemailer-sendgrid')

const sendgrid = nodemailer.createTransport(
  nodemailerSendgrid({
    apiKey: process.env.SENDGRID_API_KEY
  })
)

module.exports = sendgrid

I am not able to point out what's going wrong here. Any thoughts on why this is happening ?

Cheers

itsyogesh commented 6 years ago

Figured it out, it was an issue with the API key.

pguillory commented 5 months ago

That's awesome that you figured it out! Do you want to share your solution with everyone else having the same problem?