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

时间:2017-12-05 07:36:06

标签: 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.');
            }
        });
};

在我的实际控制器中,我尝试按以下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)

1 个答案:

答案 0 :(得分:1)

主要问题您使用了过时的示例。在示例 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