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

NodeMailer - Transport option must be a transport instance or configuration object

我正在尝试按照 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.');
            }
        });
};

在我的实际控制人中,我尝试发送邮件如下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 版本和未知 email-templates。当前 nodemailer 版本是 4.4.1.

在最新的 email-templates 版本中,您不需要直接使用 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