0

I have a trouble with Nodemailer. It works on localhost and responds with an error message. What is wrong here? React.js code example:

import React from 'react'
import styles from './index.css'

export default class Form extends React.Component {
  render() {
    return (
      <form action='/contact' method='post'>
        <h2>Contact me</h2>
        <input name='name' type='text' placeholder='Name' required />
        <input name='location' type='text' placeholder='Location' required />
        <input name='email' type='email' placeholder='Email' required />
        <textarea name='message' />
        <button type='submit'>Send</button>
      </form>
    );
  }
}

And this is a part of server.js Express file:

  app.use(middleware);
  app.use(webpackHotMiddleware(compiler));
  app.get('/', function response(req, res) {
    res.write(middleware.fileSystem.readFileSync(path.join(__dirname, '../dist/index.html')));
    res.end();
  });

  app.post('/contact', function (req, res) {
  // Setup Nodemailer transport
    var mailOpts, smtpTrans;
    smtpTrans = nodemailer.createTransport('SMTP', {
      service: 'Gmail',
      auth: {
        user: "some-my-email@gmail.com",
        pass: "password-of-thisemail"
      }
    });
    //Mail options
    mailOpts = {
      from: 'noreply@domain.io>',
      to: 'my@domain.io',
      subject: 'Website contact form',
      text: 'Hello!'
    };
    smtpTrans.sendMail(mailOpts, function (error, response) {
      // Email not sent
      if (error) {
        res.render('contact', {
          err: true, page: 'contact'
        })
      }
      // Email sent
      else {
        res.render('contact', {
          err: false, page: 'contact'
        })
      }
    });
  });
Error: No default engine was specified and no extension was provided.
   at new View (/home/azat/git/azat-io/node_modules/express/lib/view.js:62:11)
   at EventEmitter.render (/home/azat/git/azat-io/node_modules/express/lib/application.js:569:12)
   at ServerResponse.render (/home/azat/git/azat-io/node_modules/express/lib/response.js:961:7)
   at /home/azat/git/azat-io/scripts/server.js:52:13
   at Nodemailer.sendMail (/home/azat/git/azat-io/node_modules/nodemailer/lib/nodemailer.js:265:16)
   at /home/azat/git/azat-io/scripts/server.js:50:15
   at Layer.handle [as handle_request] (/home/azat/git/azat-io/node_modules/express/lib/router/layer.js:95:5)
   at next (/home/azat/git/azat-io/node_modules/express/lib/router/route.js:131:13)
   at Route.dispatch (/home/azat/git/azat-io/node_modules/express/lib/router/route.js:112:3)
   at Layer.handle [as handle_request] (/home/azat/git/azat-io/node_modules/express/lib/router/layer.js:95:5)
   at /home/azat/git/azat-io/node_modules/express/lib/router/index.js:277:22
   at Function.process_params (/home/azat/git/azat-io/node_modules/express/lib/router/index.js:330:12)
   at next (/home/azat/git/azat-io/node_modules/express/lib/router/index.js:271:10)
   at middleware (/home/azat/git/azat-io/node_modules/webpack-hot-middleware/middleware.js:39:48)
   at Layer.handle [as handle_request] (/home/azat/git/azat-io/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/home/azat/git/azat-io/node_modules/express/lib/router/index.js:312:13)

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

So, what's wrong with this code? Could you help me, please?

Guido
  • 46,642
  • 28
  • 120
  • 174

3 Answers3

0

Even if the nodemailer response with a error you should get the response. Can you try to comment the nodmailer part out and see if you get any response. it looks to me something to do with express.

Mustafa Mamun
  • 2,591
  • 2
  • 14
  • 17
0

The returned error is about Express.js not about Nodemailer.

I think the email isn't being sent because you're using the original password to your email. You have to create a new app password from your google account in order to send the mail. Refer to This answer for details.

"No default engine was specified and no extension was provided." clearly states that you haven't set a default view engine. You can find a solution to that here

KRKI
  • 1
  • 3
0

At the time of writing, Less Secure Apps is no longer supported by google. And you can't use your google account password.

You're gonna have to generate a new app password.

App passwords only work if 2-step verification is turned on. Follow this steps to get the app password

  1. Go to https://myaccount.google.com/security
  2. Enable 2 Factor Authentication
  3. Create App Password for Email
  4. Copy that password (16 characters) into the pass parameter in Nodemailer auth.
const client = nodemailer.createTransport({
    service: "Gmail",
    auth: {
        user: "username@gmail.com",
        pass: "Google-App-Password-Without-Spaces"
    }
});

client.sendMail(
    {
        from: "sender",
        to: "recipient",
        subject: "Sending it from Heroku",
        text: "Hey, I'm being sent from the cloud"
    }
)