1
1
import nodemailer from "nodemailer" ;
2
- import { emailOptions } from "../config/emailConfig" ;
2
+ import { emailConfig } from "../config/emailConfig.js" ;
3
+ import fs from "fs" ;
4
+ import path from "path" ;
5
+ import { fileURLToPath } from "url" ;
6
+ import ejs from "ejs" ;
7
+
8
+ const __filename = fileURLToPath ( import . meta. url ) ;
9
+ const __dirname = path . dirname ( __filename ) ;
3
10
4
11
const transporter = nodemailer . createTransport ( {
5
- host : emailOptions . smtp_host ,
6
- port : emailOptions . smtp_port ,
12
+ host : emailConfig . smtp_host ,
13
+ port : emailConfig . smtp_port ,
7
14
auth : {
8
- user : emailOptions . user ,
9
- pass : emailOptions . password ,
15
+ user : emailConfig . user ,
16
+ pass : emailConfig . password ,
10
17
} ,
11
18
} ) ;
12
19
@@ -18,8 +25,13 @@ transporter.verify(function (error, success) {
18
25
}
19
26
} ) ;
20
27
21
- export const sendEmail = async ( { to, subject, text, html } ) => {
22
- const emailOptions = { from : emailOptions . user , to, subject, text, html } ;
28
+ export const sendEmail = async ( { to, subject, htmlTemplateData } ) => {
29
+ const pathToTemplateHtml = path . resolve ( __dirname , "../utils/emailTemplate.html" ) ;
30
+ const template = fs . readFileSync ( pathToTemplateHtml , "utf-8" ) ;
31
+
32
+ const html = ejs . render ( template , htmlTemplateData ) ;
33
+
34
+ const emailOptions = { from : emailConfig . user , to, subject, html } ;
23
35
try {
24
36
await transporter . sendMail ( emailOptions ) ;
25
37
} catch ( err ) {
0 commit comments