Node.js Nodejs+;Nodemailer@4.6.4+;Emailtemplates@3.6.0

Node.js Nodejs+;Nodemailer@4.6.4+;Emailtemplates@3.6.0,node.js,nodemailer,email-templates,Node.js,Nodemailer,Email Templates,我试着用NodeEmailer发送邮件。它本身运行良好。但我也需要模板,所以我在谷歌上搜索了一下,找到了。我将它添加到项目中,并安装了它。都很好。我创建了一个模板,在开发模式下一切正常,但在生产模式下(在实时服务器上)一切正常 当我尝试从服务器发送邮件时,NodeEmailer出现以下错误: { Error: No recipients defined at SMTPConnection._formatError (node_modules/nodemailer/lib/smt

我试着用NodeEmailer发送邮件。它本身运行良好。但我也需要模板,所以我在谷歌上搜索了一下,找到了。我将它添加到项目中,并安装了它。都很好。我创建了一个模板,在开发模式下一切正常,但在生产模式下(在实时服务器上)一切正常 当我尝试从服务器发送邮件时,NodeEmailer出现以下错误:

{ Error: No recipients defined
at SMTPConnection._formatError          (node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
at SMTPConnection._setEnvelope (node_modules/nodemailer/lib/smtp-connection/index.js:815:34)
at SMTPConnection.send (node_modules/nodemailer/lib/smtp-connection/index.js:431:14)
at sendMessage (node_modules/nodemailer/lib/smtp-transport/index.js:226:28)
at connection.connect (node_modules/nodemailer/lib/smtp-transport/index.js:287:21)
at SMTPConnection.once (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 (node_modules/nodemailer/lib/smtp-connection/index.js:1128:14)
at SMTPConnection._processResponse (node_modules/nodemailer/lib/smtp-connection/index.js:762:20)
at SMTPConnection._onData (node_modules/nodemailer/lib/smtp-connection/index.js:558:14)
at Socket._socket.on.chunk (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' }
错误很明显。没有定义收件人,这意味着我“错过”了NodeEmailer的“收件人”参数。但我没有错过。节点管理器无法从“我的功能”中提供的邮件选项(电子邮件)中检测到它。
好的,这是我的代码

导入所需的模块

const nodemailer = require('nodemailer');
const Email = require('email-templates');
为节点创建传输程序

let transporter = nodemailer.createTransport({
        host: 'localhost',
        port: 25,
        secure: false,
        tls: {
            rejectUnauthorized: false
        }
    });
使用电子邮件模板创建新的邮件模板

  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));
    });
下一个问题是,用于外部邮件呈现的Nodemailer已被弃用,我不知道如何修复它。有人知道如何发送电子邮件和电子邮件模板吗。很抱歉给您带来不便

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

您不需要使用单独的NodeEmailer,因为电子邮件模板已经集成了NodeEmailer本身。因此,为了能够使用NodeEmailr发送电子邮件,只需使用NodeEmailr传输配置对象设置传输选项,并启用电子邮件模板的发送选项。总之,您的代码应该如下所示:

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
        }
    }
});
之后,调用函数send of email template(如其文档:):


谢谢你的答复。将测试它;)很好的解释,文档对此有点困惑,不太清楚应该在哪里插入
传输
configs和/或
节点收信人
是否仍然应该停留在初始化阶段。谢谢
email.send({
    template: 'mars',
    message: {
      to: 'elon@spacex.com'
    },
    locals: {
      name: 'Elon'
    }
  })
  .then(console.log)
  .catch(console.error);