简体   繁体   English

如何在 Nestjs API 中为 email 模板提供 static 文件

[英]How serve static file for email template in Nestjs API

I want to have an email template in the public folder.我想在公共文件夹中有一个 email 模板。 I don't know how to make the email template import/available in the send email function.我不知道如何使 email 模板导入/在发送 email function 中可用。 I am getting this error.我收到这个错误。

Cannot find module '../../public/view/email-template'.t找不到模块'../../public/view/email-template'.t

I have already added this line for nests to use public folder to serve the static files which have HTML, CSS, and js files in it.我已经为嵌套添加了这一行,以使用公共文件夹来提供 static 文件,其中包含 HTML、CSS 和 js 文件。

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

This is send email template function这是发送 email 模板 function

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: 'noreply@mail.com',
        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);
        }
    });

}
 }

In your email-template.js, write a function which exports the html as a string and then use it.在您的 email-template.js 中,编写一个 function 将 html 作为字符串导出,然后使用它。

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

In send email template function,在发送 email 模板 function,

import {emailTem} from '../../public/view/email-template';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM