最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

NodeMailer

IT培训 admin 4浏览 0评论

NodeMailer

我正在尝试按照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版本和未知的电子邮件模板。当前的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

NodeMailer

我正在尝试按照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版本和未知的电子邮件模板。当前的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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论