Nodemailer Not Working With Heroku Deployment

问题: Long story short, I wanted to add a email system to my application so I decided on Nodemailer. Everything works fine on localhost but once I try to send an email from my he...

问题:

Long story short, I wanted to add a email system to my application so I decided on Nodemailer. Everything works fine on localhost but once I try to send an email from my heroku deployed site, it fails with a 500 internal error.

I've already done the stuff with my gmail account to allow it to accept unsecure apps and stuff.

Here is my nodemailer setup:

app.post('/sendMail', (req, res) => {
  const { name, email, total } = req.body
  const nameCheck = name
    .split(' ')
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ')

  const output = `
    <p>Thanks for shopping with us, ${nameCheck}.</p>
    <p>Your Total is $${total}</p>
    <hr />
    <p>Gabriel Pozo - React Cart Developer / Owner</p>
    <a href="https://reactshoppingcart1.herokuapp.com/">Shop</a>
  `

  const transporter = nodemailer.createTransport({
    service: 'gmail',
    port: 25,
    secure: false,
    auth: {
      user: 'beyondutraining@gmail.com', 
      pass
    },
    tls: { rejectUnauthorized: false }
  })

  const mailOptions = {
    from: '"Gabriel" <beyondutraining@gmail.com>',
    to: email,
    subject: 'Your Order',
    html: output
  }

  transporter.sendMail(mailOptions, (error, info) => {
    error ? res.sendStatus(500) : res.sendStatus(200)
  })
})

This is how I'm serving static files from React:

if (process.env.NODE_ENV === 'production') {
  app.use(express.static('client/build'))

  app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
  })
}

This is how I'm assigning env variables:

module.exports = {
  PASS: process.env.PASS
}

If there's any other code you'd like, let me know!


回答1:

You didn't mention whether you have set the environment variables on your Heroku project. It should be accessible at the link https://dashboard.heroku.com/apps/<your-project-id>/settings. The Config Vars section has a Reveal Config Vars button - when pressed, you can add the key PASS and give it appropriate value.


回答2:

I've managed to solve it. I needed to use the Gmail API in order to access a clientID, clientSecret, refreshToken and accessToken. Once I retrieved those, I added them to my Heroku config variables and deployed. I then changed port to be 465, set secure to true, added host to be smtp.gmail.com and added type of Oauth2 inside the auth section.

Here is the code:

const transporter = nodemailer.createTransport({
  host: 'smtp.gmail.com',
  port: 465,
  secure: true,
  auth: {
    type: 'OAuth2',
    user: 'beyondutraining@gmail.com', 
    clientId: keys.clientID,
    clientSecret: keys.clientSecret,
    refreshToken: keys.refreshToken,
    accessToken: keys.accessToken
  }
})
  • 发表于 2018-07-12 02:09
  • 阅读 ( 585 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除