简体   繁体   中英

How serve static file for email template in Nestjs API

I want to have an email template in the public folder. I don't know how to make the email template import/available in the send email function. I am getting this error.

Cannot find module '../../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.

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

This is send email template 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.

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

In send email template function,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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