nodemailer is working locally, but it does not work when i deploy my app

Created on 14 May 2018  路  8Comments  路  Source: nodemailer/nodemailer

const express = require('express');
const nodeMailer = require('nodemailer');
const bodyParser = require('body-parser');
const cors = require('cors')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
const server = express()

server.use(bodyParser.json())
server.use(cors())

server.get('*', (req, res) => {
return handle(req, res)
})

server.post('/send-email', cors(),function (req, res) {

  let transporter = nodeMailer.createTransport({
      service: "Gmail",
      auth: {
          user: '[email protected]',
          pass: xxxxxxxxx'
      },
      tls: {
        rejectUnauthorized: false
    }
  });

  let mailOptions = {
      from: "[email protected]" , // sender address
      to: '[email protected]',
      subject: 'Email from '+req.body.name,
      html: `Name: ${req.body.name} <br /><br />
             Phone number: <a tel:${req.body.phone}>${req.body.phone}</a> <br /><br />
             Email: ${req.body.email} <br /><br />
             Pick up postcode: ${req.body.pickUp} <br /><br />
             Delivery postcode: ${req.body.delivery} <br /><br />
             Items: ${req.body.items}`
    };

   transporter.sendMail(mailOptions, (error, info) => {
       if (error) {

        res.status(400);
        res.json({ error });
       }
          console.log('Message sent: ');
           res.json({ msg: 'You email has been sent' })
        });
  });
      server.listen(3000, function(){
        console.log('Server is running at port: 3000');
      });
    })

All 8 comments

i tried with Yahoo it is the same. it gives me 501 error

same problem

There is nothing wrong with Nodemailer. It is about these services that do not trust authentications from unexpected locations, unknown IP addresses and such. If you want to make sure that your server is able to send mail through these services either switch to OAuth or use another service like SendGrid or Mailgun or Mailjet or any other.

I tried different service. locally it work fine. on deploy i got this:
Error: Invalid login: 501 Syntactically invalid EHLO argument(s)

There is something invalid in your server hostname, probably includes an underscore or some other non-valid character. You can set another hostname with the name argument when setting up SMTP settings. docs

i have underscore in docker container name. So hostname in container has underscore in name too.
@andris9 thx for quick response

ok, in this case use name argument for smtp and set it to something else, in most cases it does not matter what the hostname is, as long as it is valid and accepted by the server.

Was this page helpful?
0 / 5 - 0 ratings