catmal
catmal

Reputation: 1758

NodeMailer send HTML email with template from database

In my app I have set nodemailer to send out emails. The app is built with Vue and Firebase. I need to modify email templates from UI, so they are saved in HTML format in database. Inside these templates I need to be able to use dynamic values.

This is the method to create the email:

 const mailOptions = {
     from: "Test",
     to: req.body.email,
     subject: "New email from" + " " + req.body.email,
     html: req.body.arenile.email_template
  };

The content of req.body.arenile.email_template in FireStore is:

<p>Date: ${req.body.reservation_date} </p><p>Email: ${req.body.email}</p>

The problem is that variables are not evaluated and printed out on email as they are.

I tried:

html: ` ${req.body.arenile.email_template}`

but it doesn't solve it.

If I use:

html: `<p>Date: ${req.body.reservation_date} </p><p>Email: ${req.body.email}</p>`

it works fine. ${req.body.email} is printed as [email protected]

But:

html: ` ${req.body.arenile.email_template}`

doesn't. ${req.body.email} is printed as ${req.body.email}

I am not sure if I need to escape HTML differently or if request data are being evaluated in the wrong place..

How can I fix it?

Upvotes: 0

Views: 983

Answers (1)

catmal
catmal

Reputation: 1758

I solved this way:

var html = req.body.arenile.email_template;
html = html.replace('{{email}}', req.body.email);
html: html

Then using {{email}} will print the corrent value.

Upvotes: 1

Related Questions