node.js - NodeMailer - 传输选项必须是传输实例或配置对象

标签 node.js nodemailer email-templates

我正在尝试按照 here 中的示例创建电子邮件验证邮件程序。我添加了 email-templatenodemailer 包。我在我的应用程序中将 transporter 作为适配器使用。以下是 mailer.ts 的代码:

import * as mailer from 'nodemailer';
import * as dotenv from 'dotenv';

dotenv.load({ path: '.env' });

var mailConfig = {
    host: process.env.MAIL_HOST,
    port: process.env.MAIL_PORT,
    auth: {
        user: process.env.MAIL_USERNAME,
        pass: process.env.MAIL_PASSWORD
    }
};

var transporter = mailer.createTransport(mailConfig);

module.exports = transporter;

我正在尝试围绕 email-template 构建一个包装器,如下所示 signup.ts:

const EmailTemplate = require('email-templates');
var mailer = require('../mailer');

var sendEmailVerficationLink = mailer.templateSender(
    new EmailTemplate({
        views: { root: './verify' }
    }), {
        from: process.env.MAIL_FROM,
    });

exports.sendVerficationLink = function (obj) {
    sendEmailVerficationLink({
        to: obj.email,
        subject: 'Verify your Email'
    }, {
            name: obj.username,
            token: obj.otp
        }, function (err, info) {
            if (err) {
                console.log(err)
            } else {
                console.log('Sign up mail sent to ' + obj.email + ' for verification.');
            }
        });
};

在我的实际 Controller 中,我尝试发送如下邮件user.ts:

var verify = require('../utils/mailer-templates/signup');
signup = (req, res) => {
    ..
    verify.sendVerficationLink(obj); // send the actual user object
    ..
}

但我不断收到此错误:

Error: Transport option must be a transport instance or configuration object
[1]     at new Email (C:\Users\User\Documents\Vivavii-REST\node_modules\email-templates\lib\index.js:82:83)
[1]     at Object.<anonymous> (C:\Users\User\Documents\Vivavii-REST\dist\utils\mailer-templates\signup.js:3:54)
[1]     at Module._compile (module.js:641:30)
[1]     at Object.Module._extensions..js (module.js:652:10)
[1]     at Module.load (module.js:560:32)
[1]     at tryModuleLoad (module.js:503:12)
[1]     at Function.Module._load (module.js:495:3)
[1]     at Module.require (module.js:585:17)
[1]     at require (internal/module.js:11:18)
[1]     at Object.<anonymous> (C:\Users\User\Documents\Vivavii-REST\dist\controllers\user.js:14:14)
[1]     at Module._compile (module.js:641:30)
[1]     at Object.Module._extensions..js (module.js:652:10)
[1]     at Module.load (module.js:560:32)
[1]     at tryModuleLoad (module.js:503:12)
[1]     at Function.Module._load (module.js:495:3)
[1]     at Module.require (module.js:585:17)

最佳答案

主要问题您使用了过时的示例。在示例中,nodemailer 具有 2.7.2 版本和未知的电子邮件模板。当前的nodemailer版本是4.4.1。

在最新的电子邮件模板版本中,您不需要直接使用nodemailer。您可以在传输参数中传递配置。这是代码的固定版本:

const EmailTemplate = require('email-templates');
const dotenv = require('dotenv');

dotenv.load({ path: '.env' });

const emailTemplate = new EmailTemplate({
    message: {
        from: process.env.MAIL_FROM
    },
    transport: {
        host: process.env.MAIL_HOST,
        port: process.env.MAIL_PORT,
        auth: {
            user: process.env.MAIL_USERNAME,
            pass: process.env.MAIL_PASSWORD
        }
    }
});

function sendVerficationLink(obj) {
    return emailTemplate.send({
        template: 'verify',
        message: {
            to: obj.email
        },
        locals: {
            name: obj.username,
            token: obj.otp
        }
    });
};

exports.sendVerficationLink = sendVerficationLink;

一些细节:

  • mailer.ts 是多余的
  • 在项目中有包含模板的文件夹电子邮件。更多 infomation

关于node.js - NodeMailer - 传输选项必须是传输实例或配置对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648365/

相关文章:

php - 将 Laravel 5.4 Markdown 模板保存到数据库并从中检索

javascript - 浏览器内的Node JS fs模块

javascript - Firebase功能上的"Error: Client network socket disconnected before secure TLS connection was established"

node.js nodemailer smtpTransport 导致 TypeError : this. mailcomposer.setMessageOption 不是函数

node.js - 带有电子邮件模板的 i18n 模块

html - 如何在 HTML 电子邮件模板中嵌入图像?

node.js - 如何将我的应用程序连接到 Redis Labs 服务器?

javascript - 类型错误 : Cannot read property 'message' of undefined - Twitter API

javascript - 将 Jade Link 中的参数传递给 Node Controller

node.js - Zoho mail 显示 Node Js 中的 535 身份验证失败