首页 > 解决方案 > 部署到 heroku 时,我的 node.js/express api 未通过 sendgrid 发送电子邮件

问题描述

我已经让这个应用程序在本地运行良好。我已经将它部署到heroku,现在无法让它工作。更具体地说,当我提交联系表格时,一切似乎都正常,但电子邮件没有出现,而且似乎根本没有出现在我的 sendgrid 活动中。我已将凭据添加到 heroku,我已在 heroku 上添加了 sendgrid 插件。我什至在任何地方都看不到任何错误,让我知道为什么我没有收到电子邮件。

以下是从构建到从附加到应用程序的联系表单发出单个请求的 heroku 日志:

2020-10-07T17:16:03.000000+00:00 app[api]: Build succeeded
2020-10-07T17:16:04.348666+00:00 heroku[web.1]: Restarting
2020-10-07T17:16:04.363907+00:00 heroku[web.1]: State changed from up to starting
2020-10-07T17:16:05.685986+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-10-07T17:16:05.814303+00:00 heroku[web.1]: Process exited with status 143
2020-10-07T17:16:06.260008+00:00 heroku[web.1]: Starting process with command `npm start`
2020-10-07T17:16:08.187077+00:00 app[web.1]:
2020-10-07T17:16:08.187091+00:00 app[web.1]: > my-heroku-api@1.0.0 start /app
2020-10-07T17:16:08.187091+00:00 app[web.1]: > node app.js
2020-10-07T17:16:08.187091+00:00 app[web.1]:
2020-10-07T17:16:08.369992+00:00 app[web.1]: this is listening on port 39598
2020-10-07T17:16:09.881100+00:00 heroku[web.1]: State changed from starting to up
2020-10-07T17:16:34.484799+00:00 heroku[router]: at=info method=OPTIONS path="/email" host=my-heroku-api.herokuapp.com request_id=75fcc1cc-a4e7-4ab5-8628-8ff81d847bcb fwd="23.106.56.14" dyno=web.1 connect=0ms service=13ms status=204 bytes=301 protocol=https
2020-10-07T17:16:36.215440+00:00 app[web.1]: hello
2020-10-07T17:16:36.220749+00:00 heroku[router]: at=info method=POST path="/email" host=my-heroku-api.herokuapp.com request_id=a782762e-6ffc-40b5-b94d-3785be2a5def fwd="23.106.56.14" dyno=web.1 connect=1ms service=502ms status=200 bytes=375 protocol=https

这是我的应用程序:

require('dotenv').config()
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const sendGrid = require('@sendgrid/mail');
const app = express();

app.use(bodyParser.json());
app.use(cors());
app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Control-Type, Authorization');
    next();
});
app.get('/', (req, res) => res.send('ready to go AGAIN'));
app.post('/email', (req, res, next) => {
    sendGrid.setApiKey(process.env.API_KEY);
    const { name } = req.body;
    const { email } = req.body;
    const { message } = req.body;
    const { phone } = req.body;
    const info = `name: ${name} \n email: ${email} \n message: ${message} \n number: ${phone} `;
    const content = `
        <p>You have a new form enquiry</p>
    <h3>Contact Details </h3>
    <ul>
        <li>Name: ${name}</li>
                <li>Email: ${email}</li> 
                <li>Phone: ${phone}</li>
    </ul>
<p>They said: ${message}</p>
    `

    const msg = {
        from: 'Form Enquiry <info@website.co.uk>',
        to: '<my@email.com>',
        subject: 'a New Contact Form Enquiry',
        text: 'New form enquiry',
        html: content
    }

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

app.listen(process.env.PORT, () => console.log(`this is listening on port ${process.env.PORT}`));

我的前端:

class Contact extends Component {
constructor(props) {
    super(props);
    this.state = {
        message: "",
        name: "",
        email: "",
        phone: "",
        buttonText: "Send message",
        sent: false
    }
}

formSubmit = e => {
    e.preventDefault()
    this.setState({
        buttonText: "Sending..."
    })
    let data = {
        name: this.state.name,
        message: this.state.message,
        email: this.state.email,
        phone: this.state.phone
    }
    Axios.post('https://my-heroku-api/email', data)
        .then(res => {
            console.log(res);
            console.log("its working")
            this.setState({ sent: true }, this.resetForm())
        })
        .catch((error) => {
            console.log("this is the error", error)

            this.setState({
                message: "",
                name: "",
                email: "",
                phone: "",
                buttonText: "Send message",
            })
        })
}

resetForm = () => {
    this.setState({
        message: "",
        name: "",
        email: "",
        phone: "",
        buttonText: "Message sent!",
    })
}

哪个是用 react 和 tbh 写的,我没有碰过,并且一直工作正常,所以我假设它不在那里?我看不出我是否遗漏了一些明显的东西,但我一直盯着它并尝试了好几个小时。

任何帮助,将不胜感激!

谢谢 (:

编辑:我尝试发送的电子邮件似乎出现在我的 sendgrid 活动中,但它们没有出现在我的电子邮件收件箱中。

标签: node.jsreactjsemailherokusendgrid

解决方案


我今天在使用 Heroku 和 SendGrid 时也遇到了这个问题。这可能是 Heroku 的问题,因为直到今天晚些时候一切正常,现在我的构建失败并且仍然没有发送电子邮件。


推荐阅读