如何在Nestjs API中为邮件模板提供静态文件?

问题描述 投票:0回答:1

我想在公共文件夹中有一个邮件模板。我不知道如何在发送电子邮件功能中使电子邮件模板可导入。我得到这个错误。

找不到模块'......publicviewemail-template'.t。

我已经添加了这一行,让nests使用public文件夹来提供静态文件,其中有HTML、CSS和js文件。

  app.useStaticAssets(join(__dirname, '..', 'public'));

这是发送邮件模板功能

import nodemailer = require('nodemailer');
import sgTransport = require('nodemailer-sendgrid-transport');
import {emailTem} from '../../public/view/email-template'; //This is where I am importing the HTML template

export const SendEmail = async (email: string, link: string, name: string) => {

    const options = {
        auth: {
            api_user: process.env.SG_UserName,
            api_key: process.env.SG_Password
        }
    }


    const client = nodemailer.createTransport(sgTransport(options));

    const emailObj = {
        from: '[email protected]',
        to: email,
        subject: "Email Confirmation from Us",
        text: emailTem,
        html: emailTem
    };

    client.sendMail(emailObj, function (err, info) {
        if (err) {
            console.log(err);
        }
        else {
            console.log('Message sent: ' + link);
        }
    });

}
 }
node.js angular express nestjs email-integration
1个回答
0
投票

在你的email-template.js中,写一个函数,将html导出为一个字符串,然后使用它。

export function emailTem() {
let html =`<html></html>`;
return html;
}

在发送邮件模板函数中。

import {emailTem} from '../../public/view/email-template';
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.