Nodemailer不适用于Heroku部署

因此,我在Heroku上部署了我的网站组合,但是我的联系表(使用nodemailer)无法正常工作。这很奇怪,因为当我从计算机上尝试时,我收到了一封电子邮件,但从其他人那里得知它无法正常工作。这是我的index.js的代码:

var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
var cors = require('cors');
const creds = require('./config');

var transport = {
    host: 'smtp.gmail.com', 
    port: 465,
    auth: {
    user: creds.USER,
    pass: creds.PASS
  }
}

var transporter = nodemailer.createTransport(transport)

transporter.verify((error, success) => {
  if (error) {
    console.log(error);
  } else {
    console.log('Server is ready to take messages');
  }
});

router.post('/send', (req, res, next) => {
  var name = req.body.name
  var email = req.body.email
  var message = req.body.message
  var content = ` name: ${name} \n email: ${email} \n message: ${message} `

  var mail = {
    from: name,
    to: 'js5360@columbia.edu', 
    subject: 'New Message from Contact Form',
    text: content
  }

  transporter.sendMail(mail, (err, data) => {
    if (err) {
      res.json({
        status: 'fail'
      })
    } else {
      res.json({
       status: 'success'
      })
    }
  })
})

const app = express()
app.use(cors())
app.use(express.json())
app.use('/', router)
app.listen(3002)

以前,当我在localhost上工作时,我已经启用了对安全性较低的应用程序的访问,因此它在本地运行良好。 我还需要做什么?

评论