使用Express App的Nodemailer无法通过按钮发送电子邮件

时间:2015-05-31 20:06:33

标签: javascript node.js email nodemailer

使用Nodemailer中的默认设置,当应用启动并且app.js页面上包含所有代码时,电子邮件将与我们的内部邮件服务器一起发送。

//app.js
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
  host: 'mail.oursite.com',
});
var mailOptions = {
  from: 'couponrequest@company.com', // sender address
  to: 'myaddy@company.com', // list of receivers
  subject: 'Hello ✔', // Subject line
  text: 'Hello world ✔', // plaintext body
  html: '<b>Hello world ✔</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info) {
  if (error) {
    return console.log(error);
  }
  console.log('Message sent: ' + info.response);
});

然后当切换它时,它会中断并且在尝试通过按钮点击发生时没有任何反应。

App.js

var nodemailer = require('nodemailer');
app.get('/users', routes.users);

Users.js

var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');

/*
 * POST to addcoupon Request.
 */
router.post('/addcoupon', function(req, res) {
  // create reusable transporter object using SMTP transport
  var transporter = nodemailer.createTransport({
    host: 'mail.primeshine.com',
  });
});
module.exports = router;

Index.js

var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');

/*
 * POST to addcoupon Request.
 */
router.post('/addcoupon', function(req, res) {
  // create reusable transporter object using SMTP transport
  var transporter = nodemailer.createTransport({
    host: 'mail.primeshine.com',
  });
});
module.exports = router;

Global.js

// Add Coupon Request
function addCoupon(event) {
  event.preventDefault();

  var mailOptions = {
    from: 'couponrequest@company.com', // sender address
    to: 'myaddy@company.com', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world ✔', // plaintext body
    html: '<b>Hello world ✔</b>' // html body
  };

  // send mail with defined transport object
  transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
      return console.log(error);
    }
    console.log('Message sent: ' + info.response);
  });
};

我尝试仅使用相关代码发送它。当它添加用户信息时,它可以很好地向MongoDB发送信息,但当我尝试发送电子邮件时它没有提交任何内容。不确定我是否有错误的地方或其他问题...提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

当我使用nodemailer从我的电子邮件帐户发送电子邮件时,我必须使用身份验证创建传输:

/* define transportation */
var transport = nodemailer.createTransport(smtpTransport({
    host: 'posteo.de',
    port: 465,
    secure: true,
    auth: {
        user: 'username',
        pass: 'password'
    },
    maxConnections: 5,
    maxMessages: 10
}));

在你的情况下不确定这是否是必要的,但也许有帮助。