简体   繁体   中英

Nodemailer doesn't work, what's wrong?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM