如何使用nodemail和smtp传输sendMail?

时间:2016-12-21 16:10:48

标签: node.js email smtp nodemailer

一旦我从客户端获取用户详细信息,我就有新的用户注册我正在向组发送电子邮件以向用户授予权限,下面的代码抛出异常,任何想法是什么实施错误?

我已经粘贴了有问题的错误。

Mailer.js

var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');

var MAIL_SERVER_URL = '127.0.0.0'; 


//establish the transporter
var transporter = nodemailer.createTransport(smtpTransport({
    host: MAIL_SERVER_URL,
    port: 80,
    connectionTimeout: 60000,
}));

var Mailer = {
      options: function (mailOptions) {
        mailOptions.to = 'group@gmail.com';
        mailOptions.text = mailOptions.auid + ''+ 'requested access for modeler';
        console.log('mailOptions',mailOptions);
        transporter.sendMail(mailOptions, function(error, info) {
                    if (error) {
                        return console.log(error);
                    } else {
                        console.log('Message sent: ' + info.response);
                    }
        });
      }
}



module.exports = Mailer;

错误

{ [Error: connect EACCES 127.0.0.0:25]
  code: 'ECONNECTION',
  errno: 'EACCES',
  syscall: 'connect',
  address: '127.0.0.0',
  port: 80,
  command: 'CONN' }

1 个答案:

答案 0 :(得分:2)

您的错误输出告诉您nodemailer无法访问端口25上127.0.0.0的SMTP服务器。

使用SMTP实现nodemailer看起来很好,假设传入的mailOptions具有from属性。您可以在the docs中通过nodemailer了解有关SMTP的更多信息。

let nodemailer = require('nodemailer');

let MAIL_SERVER_URL = '127.0.0.0';
let smtp = nodemailer.createTransport({
    host: MAIL_SERVER_URL,
    connectionTimeout: 60000
});

module.exports = {
    options: (mailOptions) => {
        let email = {
            from: 'from@test.com',
            to: 'group@gmail.com',
            subject: 'yourEmailSubject'
            text: `${mailOptions.auid} requested access for modeler`
        };
        smtp.sendMail(email, (err, info) => {
            if (err) 
                console.log(err);
            else
                console.log(`Message sent: ${info.response}`);

        });
    }
};