node.js @sendgrid/mail 错误:未经授权
Posted
技术标签:
【中文标题】node.js @sendgrid/mail 错误:未经授权【英文标题】:node.js @sendgrid/mail Error: Unauthorized 【发布时间】:2019-05-20 02:10:06 【问题描述】:我想从我的应用程序 (Angular) 发送电子邮件。 我尝试在我的后面使用 @sendgrid/mail 和 node.js,但它不起作用。
错误是:
(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
也许有人可以帮助我?
请在下面找到我的 app.js
var sendgrid = require('@sendgrid/mail');
app.post('/sendemail', (request, response) =>
const body = request.body;
const email = body.email;
console.log('sendMail');
SENDGRID_APY_KEY = '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';
sendgrid.setApiKey(process.env.SENDGRID_APY_KEY);
const msg =
to: 'ambre.naude@infogene.fr',
from: 'infogene69@gmail.com',
subject: 'Votre mot de passe provisoire',
text: 'Bonjour !',
html: '<p>Bonjour!</p><p>Voici votre mot de passe provisoire:</p> <p>Cordialement,</p> <p>Infogène Lyon</p>',
;
sendgrid.send(msg);
console.log(msg);
);
【问题讨论】:
【参考方案1】:问题出在下面的部分
SENDGRID_APY_KEY = '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';
sendgrid.setApiKey(process.env.SENDGRID_APY_KEY);
在启动服务器时,需要将 sendgrid api 密钥作为环境变量传入。上面的代码查找SENDGRID_APY_KEY
作为环境变量。
要在不传入环境变量的情况下使其工作:
删除 process.env
并将 SENDGRID_APY_KEY 声明为变量。
var SENDGRID_APY_KEY = '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';
sendgrid.setApiKey(SENDGRID_APY_KEY);
将 SENDGRID_APY_KEY 作为环境变量传递:
SENDGRID_APY_KEY='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' node app.js // or however you are starting up the server
如果您确实将密钥作为环境变量传递,则需要从该环境变量设置 sendgrid api,如下所示:sendgrid.setApiKey(process.env.SENDGRID_APY_KEY)
此外,您应该重置/更新您的 API 密钥(假设您发布的密钥是您当前的密钥)
【讨论】:
如果它直接进入垃圾邮件文件夹,请联系 sendgrid 看看他们是否可以提供帮助/切换您的出站 IP 地址(假设您使用共享 IP)。 对我来说太棒了。感谢您的解决方案以上是关于node.js @sendgrid/mail 错误:未经授权的主要内容,如果未能解决你的问题,请参考以下文章
在“nodejs”中使用“@sendgrid/mail”发送“base64”电子邮件“附件”时遇到问题