【问题标题】:Nodejs +Nodemailer @4.6.4 + Emailtemplates @3.6.0Nodejs +Nodemailer @4.6.4 + 电子邮件模板 @3.6.0
【发布时间】:2018-10-07 08:11:30
【问题描述】:

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

{ 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' }

错误很明显。没有定义收件人,这意味着我“错过”了 Nodemailer 的“to”参数。但我没有错过。 Nodemialer 无法从我的函数中给出的 Mailoptions(电子邮件)中检测到它。
好的,这是我的代码。

导入需要的模块

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

为节点创建传输器

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

使用 email-template 创建一个新的 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));
    });

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

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

【问题讨论】:

    标签: node.js nodemailer email-templates


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。将对其进行测试;)
    • 很好的解释,文档对此有点困惑,不清楚应该在哪里插入 transport 配置和/或 nodemailer 是否仍然应该留在初始化.谢谢。 =]
    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 2016-07-18
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多