gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:09:09 26 4
gpt4 key购买 nike

我正在尝试按照 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com