Nodemailer无法正常工作,有什么不对?

时间:2016-03-17 13:30:13

标签: javascript node.js express reactjs nodemailer

我遇到了Nodemailer的问题。它适用于localhost并响应错误消息。这有什么不对? React.js代码示例:

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>
    );
  }
}

这是 server.js Express文件的一部分:

  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)

那么,这段代码有什么问题? 你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

即使nodemailer响应有错误,您也应该得到响应。你可以尝试评论nodmailer部分,看看你是否得到任何回应。它在我看来与快递有关。