【发布时间】:2020-01-13 12:13:52
【问题描述】:
我在部署后在 heroku 和 sendgrid 之间遇到了一些问题,我无法从我的表单发送电子邮件,在本地主机中一切正常,但在实时版本中有一个 400 错误的请求,有人知道发生了什么吗?
我已经尝试过了:在我的配置中,我设置了所有 SENDGRID_API_KEY 例如,如下所示
https://devcenter.heroku.com/articles/sendgrid#provisioning-the-add-on
1)heroku 插件:创建 sendgrid:starter
2) 设置 SENDGRID_API_KEY 并重新启动 ⬢ gammefive...完成,v18
一步一步但仍然无法正常工作,有些东西我看不到。 在本地,我在收件箱中看到我的电子邮件,一切正常 100% 正常
非常感谢
const router = express.Router();
const sgMail = require('@sendgrid/mail');
router.post('/api/contact', async (req, res) => {
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const email = req.body.email;
const name = req.body.name;
const message = req.body.message;
const msg = {
to: process.env.GAMMEFIVE_EMAIL,
from: email,
subject: 'Email from gammefive',
text: `name: ${name};
email:${email}; message:${message}`
};
await sgMail
.send(msg)
.then(result => {
res.status(200).send(result);
})
.catch(error => {
res.status(400).send(error);
});
});
module.exports = router; ```
**FRONT-END**
sendData = async e => {
const { name, email, message } = this.state;
e.preventDefault();
const data = {
name,
email,
message
};
await axios
.post("/api/contact", data)
.then(res => {
if (res.status === 200) {
this.setState({
alertMessage: ""
});
}
})
.catch(error => {
console.log("important error", error);
});
};
【问题讨论】:
-
请edit您的问题并向我们展示您配置的相关部分。
-
嗨@Chris,谢谢我已经发送了一些代码,但是一旦我推送到heroku,这一切正常
-
@Chris 我发现了我要写的问题作为答案,因为我想象有些人在社区中遇到了同样的问题。谢谢
标签: javascript node.js reactjs heroku sendgrid