Friday 9 June 2017

Nodemailer - Error: Greeting never received

I am running a Node server on Heroku to support a mobile app. When users submit a feedback form in the app, a POST request goes to <myapp>.herokuapp.com/feedback, with the feedback message.

I would like to use Nodemailer to send myself an email when the form is submitted. However, I get the following error instead:

{ Error: Greeting never received
    at SMTPConnection._formatError (/app/node_modules/smtp-connection/lib/smtp-connection.js:528:15)
    at SMTPConnection._onError (/app/node_modules/smtp-connection/lib/smtp-connection.js:514:16)
    at SMTPConnection.<anonymous> (/app/node_modules/smtp-connection/lib/smtp-connection.js:455:18)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5) code: 'ETIMEDOUT', command: 'CONN' }

Here is the full code for my /feedback endpoint:

const nodemailer = require('nodemailer')
const smtpTransport = require('nodemailer-smtp-transport')
const validateEmail = require('../util').validateEmail

const config = {
  host: 'securemail.webnames.ca',
  port: 587,
  secure: false, // secure:true for port 465, secure:false for port 587
  auth: {
    user: process.env.EMAIL_USER,
    pass: process.env.EMAIL_PASS,
  },
}

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport(smtpTransport(config))

// This is the endpoint callback:
module.exports = function feedback(req, res) {

  // setup email data with unicode symbols
  let mailOptions = {
    // from: `"${userName}" <${userEmail}>`,
    from: 'hello@<myapp>.com',
    to: 'hello@<myapp>.com',
    subject: 'Feedback',
    text: req.body.message,
    html: req.body.message,
  }

  // send mail with defined transport object
  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      res.setHeader('Content-Type', 'application/json')
      res.send({ success: false, error })
      return console.error(error)
    }

    console.log('Message %s sent: %s', info.messageId, info.response)

    res.setHeader('Content-Type', 'application/json')
    res.send({ success: true })
  })
}

I am using a generic email host to send emails (webnames.ca). Emails sent with a desktop mail client go through fine.

Any ideas?



via Bogdan Balan

No comments:

Post a Comment