簡體   English   中英

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

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

我想在公共文件夾中有一個 email 模板。 我不知道如何使 email 模板導入/在發送 email function 中可用。 我收到這個錯誤。

找不到模塊'../../public/view/email-template'.t

我已經為嵌套添加了這一行,以使用公共文件夾來提供 static 文件,其中包含 HTML、CSS 和 js 文件。

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

這是發送 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);
        }
    });

}
 }

在您的 email-template.js 中,編寫一個 function 將 html 作為字符串導出,然后使用它。

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

在發送 email 模板 function,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM