如何使用 nodemailer 将 express-handlebars 配置为 link?

How to configure express-handlebars to link it with nodemailer?

好吧,我有一个 API,它通过 Nodemailer 发送电子邮件。我希望将电子邮件呈现为 HTML,因此我尝试将 nodemailer-express-handlebars 连接到我的 api:

const nodemailer = require('nodemailer')
const hbs = require('nodemailer-express-handlebars)

const sendMail = async (options) => {
    try {
        const transporter = nodemailer.createTransport({
            host: process.env.EMAIL_HOST,
            port: process.env.EMAIL_PORT,
            auth: {
                user: process.env.EMAIL_USER,
                pass: process.env.EMAIL_PASS,
            },
        })

        transporter.use(
            'compile',
            hbs({
                viewEngine: {
                    extName: '.hbs',
                    partialsDir: './views/', 
                    layoutsDir: './views/layouts',
                    defaultLayout: '',
                },
                extName: '.hbs',
                viewPath: 'views',
            })
        )
        const mailOptions = {
            from: 'abc@abc.com',
            // to: 'cde@cde.com',
            to: options.email,
            bcc: 'me@me.com',
            subject: 'Booking confirmation',
            template: 'emailHtml',
        }

        await transporter.sendMail(mailOptions)
    } catch (err) {
        return new AppError(
            'There was an error while sending the email. Please, try again later.',
            500
        )
    }
}

这是我的 api 文件结构(api 本身在 email.js 中,而 util 文件夹在根目录中):

在 main.hbs 中只有标准的 html5 标记,其正文中带有 {{{body}}} 并且在 emailHtml.hbs 中只有 h1 标签中的 hello world。

但是,它不起作用(我认为问题可能出在错误的路径上,但我尝试了很多组合)。我知道有一种方法可以使用样式和变量来指定 html: <h1> hello world </h1> 但这对我来说不是一个合适的解决方案,所以模块化的解决方案对我很有帮助。当我删除 hbs 配置并在 mailOptions 中仅放入纯文本时,电子邮件已发送,但一旦我放入模板,它就会停止工作。 dotenv已经配置好了,只是没放这里

有什么想法吗?

如果 email.js 和布局文件夹位于同一文件夹中,则以下代码将起作用。

const nodemailer = require('nodemailer') 
const hbs =require('nodemailer-express-handlebars)

const sendMail = async (options) => {
    try {
        const transporter = nodemailer.createTransport({
            host: process.env.EMAIL_HOST,
            port: process.env.EMAIL_PORT,
            auth: {
                user: process.env.EMAIL_USER,
                pass: process.env.EMAIL_PASS,
            },
        })

        transporter.use(
            'compile',
            hbs({
                viewEngine: {
                    extName: '.hbs',
                    partialsDir: 'layouts/', 
                    layoutsDir: 'layouts/',
                    defaultLayout: 'emailHtml',
                },
                extName: '.hbs',
                viewPath: 'layouts/',
            })
        )
        const mailOptions = {
            from: 'abc@abc.com',
            // to: 'cde@cde.com',
            to: options.email,
            bcc: 'me@me.com',
            subject: 'Booking confirmation',
            template: 'emailHtml',
        }

        await transporter.sendMail(mailOptions)
    } catch (err) {
        return new AppError(
            'There was an error while sending the email. Please, try again later.',
            500
        )
    } }