我的node.js / express api部署到heroku时未通过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',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: "",})
        })
}

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

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

任何帮助将不胜感激!

谢谢(:

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

解决方法

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

,

我遇到了同样的问题。邮件是垃圾邮件,垃圾邮件,我的其他邮件。我不确定它是否可以帮助你,但只是分享我的想法:) 希望它已经解决了。