node.js - 403 禁止在 Node.js 中使用 sendgrid 发送电子邮件

标签 node.js reactjs express sendgrid

我正在设计一个使用 React 呈现 UI 的联系人页面。我有一个表格,应该在提交时发送电子邮件。下面是处理提交的 UI 代码:

    handleSubmit = (event) => {
    event.preventDefault();

    this.setState({
        disabled: true
    });

    Axios.post('http://localhost:3040/api/email', this.state)
        .then( res => {
            if(res.data.success){

              this.setState({
                disabled: false,
                emailSent: true
              });
            } else{
                this.setState({
                    disabled: false,
                    emailSent: false
                });
            }
        })
        .catch(err => {
            this.setState({
                disabled: false,
                emailSent: false
            });
        });
}

发送电子邮件的 api 是用 Node.js 编写的。使用@sendgrid//mail 来触发发送。在调试时,我可以看到表单值正在到达 api,但在发送时会引发 403 Forbidden 错误。 api代码如下:

app.post('/api/email', (req, res, next) => {
sendGrid.setApiKey('<Generated key in sendgrid>');
const msg = {
    to: 'some@email.com',
    from: req.body.email,
    subject: 'Website Contact Page',
    text: req.body.message
}

sendGrid.send(msg).then(result => {
    res.status(200).json({
        success: true
    });
})
.catch(err => {
    console.log('error: ', err);
    res.status(401).json({
        success: false
    });
});
});

以下是我在调试时在 VSCode 控制台中得到的错误跟踪:

stack:"Error: Forbidden
at axios.then.catch.error (c:\react\portfolio-api\node_modules\@sendgrid\client\src\classes\client.js:105:29)
at process._tickCallback (internal/process/next_tick.js:68:7)"

proto:错误 {constructor: , toString: , toJSON: }

不知道为什么它给我禁止错误。如果我需要在此处添加更多信息,请告诉我。在此先感谢:)

编辑:- 按照 sendgrid 的文档创建 API key 并在 sendGrid.setApiKey() 中使用相同的 key 。

enter image description here

最佳答案

为了能够从 sendgrid 发送电子邮件,您需要设置单发件人验证或域验证。

请查看docs验证发件人。

To ensure our customers maintain the best possible sender reputations and to uphold legitimate sending behavior, we require customers to verify their Sender Identities. A Sender Identity represents your “From” email address—the address your recipients will see as the sender of your emails.

You can verify one or more Sender Identities using either Domain Authentication or Single Sender Verification.

在您的 api 应用程序控制台日志中,错误消息必须是这样的: (要在reactjs端看到真正的错误信息,需要使用err.response.data

The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved.

关于node.js - 403 禁止在 Node.js 中使用 sendgrid 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61195201/

相关文章:

android - 在React Native项目运行期间停止下载Gradle

node.js - 如何解决 Pug 中 Json 数组的迭代问题

javascript - Node.js 中的 URL 路由

javascript - Webpack 配置的 React 组件不受 css 影响

javascript - 那么数组推送在 Promise 中不起作用吗?

node.js - 如何在 Node Express 中将表数据存储在缓存中?

postgresql - 如何从表中删除已超过 PostgreSQL 过期限制的所有条目?

javascript - 无法将数组作为 javascript 函数的参数传递

javascript - 如何在云函数中查询 Firebase 数据库?

mysql - 有没有办法在mysql npm中设置 'max_allowed_packet'?