@@ -4,9 +4,10 @@ import { fileURLToPath } from "url";
44
55import ejs from "ejs" ;
66import createError from "http-errors" ;
7+ import mjml2html from "mjml" ;
78import nodemailer from "nodemailer" ;
89
9- import { MESSAGES } from "../config/constants.js" ;
10+ import { MESSAGES , CODE } from "../config/constants.js" ;
1011import env from "../config/env.js" ;
1112
1213const filename = fileURLToPath ( import . meta. url ) ;
@@ -20,28 +21,34 @@ const transporter = nodemailer.createTransport({
2021 } ,
2122} ) ;
2223
23- const readEmailTemplate = async ( { message, url } ) => {
24+ const readEmailTemplate = async ( { code , message, url } ) => {
2425 try {
25- if ( url === "" ) {
26- const filePath = path . join ( dirname , "../views/ errorTemplate.ejs" ) ;
26+ if ( code === CODE . ERROR . FAILED_ANALYZE ) {
27+ const filePath = path . join ( dirname , "../views" , " errorTemplate.mjml .ejs") ;
2728 const template = await fs . readFile ( filePath , "utf-8" ) ;
2829 const htmlContent = ejs . render ( template , { message } ) ;
30+ const { html } = mjml2html ( htmlContent , {
31+ validationLevel : "strict" ,
32+ } ) ;
2933
30- return htmlContent ;
34+ return html ;
3135 }
32- const filePath = path . join ( dirname , "../views/ emailTemplate.ejs" ) ;
36+ const filePath = path . join ( dirname , "../views" , " emailTemplate.mjml .ejs") ;
3337 const template = await fs . readFile ( filePath , "utf-8" ) ;
34- const htmlContent = ejs . render ( template , { message, url } ) ;
38+ const htmlContent = ejs . render ( template , { url } ) ;
39+ const { html } = mjml2html ( htmlContent , {
40+ validationLevel : "strict" ,
41+ } ) ;
3542
36- return htmlContent ;
43+ return html ;
3744 } catch ( err ) {
3845 throw createError . InternalServerError ( MESSAGES . ERROR . FAILED_READ_TEMPLATE ) ;
3946 }
4047} ;
4148
42- const sendEmail = async ( { email, message, url } ) => {
49+ const sendEmail = async ( { email, code , message, url } ) => {
4350 try {
44- const html = await readEmailTemplate ( { message, url } ) ;
51+ const html = await readEmailTemplate ( { code , message, url } ) ;
4552 const mailOptions = {
4653 from : env . email_user ,
4754 to : email ,
0 commit comments