Nodejs + Nodemailer @ 4.6.4 + Emailtemplates @ 3.6.0

时间:2018-04-26 14:55:48

标签: node.js nodemailer email-templates

我尝试使用Nodemailer发送邮件。 Nodemailer本身工作正常。但我也需要模板,所以我搜索了一下它并找到了email-templates。我把它添加到项目中,安装它。一切都很好。我创建了一个模板,一切都在开发模式下正常工作但不是生产意味着(在实时服务器上) 当我尝试从我的实时服务器发送邮件时,Nodemailer会出现以下错误:

{ Error: No recipients defined
at SMTPConnection._formatError          (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
at SMTPConnection._setEnvelope (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-connection/index.js:815:34)
at SMTPConnection.send (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-connection/index.js:431:14)
at sendMessage (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-transport/index.js:226:28)
at connection.connect (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-transport/index.js:287:21)
at SMTPConnection.once (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-connection/index.js:188:17)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at SMTPConnection.emit (events.js:208:7)
at SMTPConnection._actionEHLO (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-connection/index.js:1128:14)
at SMTPConnection._processResponse (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-connection/index.js:762:20)
at SMTPConnection._onData (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-connection/index.js:558:14)
at Socket._socket.on.chunk (/Users/raphael/LocalDev/Avarius.net_Backend/node_modules/nodemailer/lib/smtp-connection/index.js:510:47)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12) code: 'EENVELOPE', command: 'API' }

错误很明显。没有收件人定义,这意味着我"错过" "到" Nodemailer的参数。但我不会错过它。 Nodemialer无法通过我的功能中的Mailoptions(电子邮件)检测到它 好的,这是我的代码。

导入所需的模块

const nodemailer = require('nodemailer');
const Email = require('email-templates');

为节点

创建传输器
let transporter = nodemailer.createTransport({
        host: 'localhost',
        port: 25,
        secure: false,
        tls: {
            rejectUnauthorized: false
        }
    });

使用电子邮件模板

创建新的Mailtemplate
  const email = new Email({
        template: '../Services/Mail/emails/activation',

        message: {
            from: from,
            subject: subject,
            to: userEmail,

        },
        locals: {
            username: username,
            url: url
        },

        transport: {
            jsonTransport: true,
            to: userEmail,

        }
    });

使用Nodemailer发送邮件

  transporter.sendMail(email, (error, info) => {
        if (error) {
            console.log(email);
            return console.log(error);
        }
        console.log('Message sent: %s', info.messageId);

        console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
    });

下一个问题是,外部Mailrendering的Nodemailer tutorial已弃用,我不知道如何修复它。有谁知道如何发送E-Mails mit Nodemailer和电子邮件模板。很抱歉给您带来不便。

Nodemailer版本:4.6.4 电子邮件模板版本:3.6.0

1 个答案:

答案 0 :(得分:1)

您不需要使用单独的Nodemailer,因为email-templates已经集成了Nodemailer。因此,为了能够使用Nodemailer发送电子邮件,只需使用Nodemailer传输配置对象设置传输选项,并启用email-templates的发送选项。总而言之,您的代码应如下所示:

const email = new Email({
    template: '../Services/Mail/emails/activation',

    message: {
        from: from,
        subject: subject,
        to: userEmail,

    },
    locals: {
        username: username,
        url: url
    },
    send: true,
    transport: {
        host: 'localhost',
        port: 25,
        secure: false,
        tls: {
             rejectUnauthorized: false
        }
    }
});

之后,调用email-template的函数send(如文档中所示:https://email-templates.js.org/#/):

email.send({
    template: 'mars',
    message: {
      to: 'elon@spacex.com'
    },
    locals: {
      name: 'Elon'
    }
  })
  .then(console.log)
  .catch(console.error);