0

The problem is that my code works fine locally but in heroku it doesn't I changed the way I am calling the sendmail function it still does the same

tried calling my function differently, nothing works

I expected to get the console to log sent then I know that the outgoing mail has been sent successfully

here it is in index.js

app.post('/', emailUtility.sendMail, function(req, res) {

  res.send("recieved your request!");
});

here's the module I'm calling

var nodemailer = require('nodemailer');

    exports.sendMail = function(req, res, next) {
      var input = JSON.parse(JSON.stringify(req.body));
      var data = {
        persons_name: input.name,
        personal_message: input.message
      };

      console.log("name :" + data.persons_name);
      console.log("message :" + data.personal_message);
      // Generate test SMTP service account from ethereal.email
      // Only needed if you don't have a real mail account for testing
      nodemailer.createTestAccount((err, account) => {
        // create reusable transporter object using the default SMTP transport
        let transporter = nodemailer.createTransport({
          host: 'smtp.gmail.com',
          port: 587,
          secure: false, // true for 465, false for other ports
          auth: {
            user: process.env.USERNAME, // generated ethereal user
            pass: process.env.PASS // generated ethereal password
          }
        });

        // setup email data with unicode symbols
        let mailOptions = {
          from: data.persons_name + '" " <example@gmail.com>', // sender address
          to: process.env.MAILTOSENDTO, // list of receivers
          subject: 'Art Life Clothing ✔', // Subject line
          text: data.personal_message, // plain text body
          html: '<b>' + data.personal_message + '</b>' // html body
        };

        // send mail with defined transport object
        transporter.sendMail(mailOptions, (error, info) => {
          if (error) {
            return console.log(error);
          }
          console.log('Message sent: %s', info.messageId);
          // Preview only available when sending through an Ethereal account
          console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));

          // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
          // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
        });
      });
      next()
    }

The error I am getting in heroku:

 at=info method=POST path="/" host=calm-island-58460.herokuapp.com request_id=ca397146-1296-42a8-a505-b2c6ebb1543a fwd="197.229.3.109" dyno=web.1 connect=0ms service=152ms status=200 bytes=222 protocol=https
2018-12-30T17:06:32.673329+00:00 app[web.1]: dns.js:229
2018-12-30T17:06:32.673336+00:00 app[web.1]:       throw new Error('"callback" argument must be a function');
2018-12-30T17:06:32.673338+00:00 app[web.1]:       ^
2018-12-30T17:06:32.673340+00:00 app[web.1]: 
2018-12-30T17:06:32.673344+00:00 app[web.1]: Error: "callback" argument must be a function
2018-12-30T17:06:32.673346+00:00 app[web.1]:     at Object.query [as resolve4] (dns.js:229:13)
2018-12-30T17:06:32.673348+00:00 app[web.1]:     at resolver (/app/node_modules/nodemailer/lib/shared/index.js:15:28)
2018-12-30T17:06:32.673350+00:00 app[web.1]:     at Object.module.exports.resolveHostname (/app/node_modules/nodemailer/lib/shared/index.js:55:5)
2018-12-30T17:06:32.673351+00:00 app[web.1]:     at SMTPConnection.connect (/app/node_modules/nodemailer/lib/smtp-connection/index.js:314:27)
2018-12-30T17:06:32.673353+00:00 app[web.1]:     at getSocket (/app/node_modules/nodemailer/lib/smtp-transport/index.js:262:24)
2018-12-30T17:06:32.673355+00:00 app[web.1]:     at Immediate.setImmediate (/app/node_modules/nodemailer/lib/smtp-transport/index.js:70:35)
2018-12-30T17:06:32.673357+00:00 app[web.1]:     at runCallback (timers.js:570:20)
2018-12-30T17:06:32.673358+00:00 app[web.1]:     at tryOnImmediate (timers.js:550:5)
2018-12-30T17:06:32.673360+00:00 app[web.1]:     at processImmediate [as _immediateCallback] (timers.js:529:5)
2018-12-30T17:06:32.767524+00:00 heroku[web.1]: State changed from up to crashed
2018-12-30T17:06:32.749958+00:00 heroku[web.1]: Process exited with status 1

1 Answers1

0

Shouldn't the next() call be inside the transporter.sendMai() ? Because this is async code and takes time to get a response and in localhost is faster.

I think with this piece of code, it may work.

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    next(error);
  }
  console.log('Message sent: %s', info.messageId);
  // Preview only available when sending through an Ethereal account
  console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));

  // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
  // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
  next()
});
Pedro Silva
  • 2,655
  • 1
  • 15
  • 24
  • It still does the same thing and I thank the fact that you made it clear that localhost is fast of which when I run it locally it waits till the message show up saying sent. It would be nice if I could get another way of executing the sendmail function which may not block and break my methods. – Oyamasiphula Dec 30 '18 at 21:57
  • You can use promises like in https://github.com/nodemailer/nodemailer/blob/master/examples/await.js because callbacks aren't used so much due to some problems that I can explain if you need. Promises with async/await are the way – Pedro Silva Dec 30 '18 at 22:00
  • I am getting this error saying unexpected token `function` next to async – Oyamasiphula Dec 31 '18 at 11:06
  • Sorry, forgot about that, here is how you solve that problem https://stackoverflow.com/a/41325619/9661304 – Pedro Silva Dec 31 '18 at 12:37