0
votes

I am trying to deploy a simple Node app that sends an email on a webhook from an external service. I can get it to run fine on my machine but once I deploy it to heroku I get a ERRCONNREFUSED on every post request. I've also tried setting the port to 465 for the createTransport function and still receive the same error.

Error

{ Error: connect ECONNREFUSED 127.0.0.1:587 at _exceptionWithHostPort (util.js:1044:20) at Object._errnoException (util.js:1022:11) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14) code: 'ECONNECTION', errno: 'ECONNREFUSED',syscall: 'connect',address: '127.0.0.1', port: 587, command: 'CONN' }

index.js

const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const app = express();

var port = process.env.PORT || 8800;

app.use(bodyParser.json());

app.post('/', function(req, res) {
   var messageBody = {};
   messageBody.name = req.body.name;
   messageBody.amount = req.body.amount_in_cash;
   messageBody.phone = req.body.phone;
   messageBody.email = req.body.email;

   if (!req.body.uid) {
    return console.log("No UID");
   }
   var transporter = nodemailer.createTransport({
       service : 'smtp.gmail.com',
       auth : {
          user : '[email protected]',
          pass : 'xxxxxx'
       }
   });

   var mailOptions = {
      from: '[email protected]',
      to : '[email protected]',
      subject : "New Donation Lead",
      text : `Name: ${messageBody.name}\nPhone: ${messageBody.phone}\nEmail: 
${messageBody.email}\nAmount: ${messageBody.amount}`
   }

  transporter.sendMail(mailOptions, function(error, info) {
      if(error) {
        console.log(error);
      } else {        
        console.log("message sent");
      }
  });

  res.send(200).end();
});



app.listen(port, () => console.log('Listening on port: '+ port));
1

1 Answers

1
votes

Little late but I found out what was happening. When deploying node applications on Heroku you have to use one of the available email "plugins". Heroku doesn't allow their servers to access mail of any kind without these.