1

I'm making a node.js server which sends emails on demand. I'm importing an html file, which I assign to "output". This is then emailed to the receiver.

app.post("/send", (req, res) => {
    console.log("sending email...");
    const token = req.body.data.token;
    const output = fs.readFileSync('emailcontents.html').toString();

This works. But what would be the best way to pass the "token" variable to emailcontents.html?

5
  • When you build an endpoint where the html file can grab this? Commented Dec 12, 2020 at 16:08
  • @Alex What do you mean? Commented Dec 12, 2020 at 16:10
  • wait I will do an answer but I'm not sure if it is the best way Commented Dec 12, 2020 at 16:12
  • how to you import the html file and how you send the email out? Commented Dec 12, 2020 at 16:16
  • @SJ19 I updated my answer, and ejs work in any case unless you don't want to install ejs package. Commented Dec 16, 2020 at 1:34

1 Answer 1

2

If I misunderstand your requirement, please let me know. I'll do my best to modify the answer.

According to the conversation in comments, we could know you want to send a email to the user instead of using res.render to render emailcontents.html content for user in browser.
That email should contain the token.
And you want to insert token into your emailcontents.html instead appending.

We assume your html content look like this.

<h1>weclome here</h1>
<p> Please use this token to do something</p>.

You want to insert token into {here}.

<h1>weclome here</h1>
<p> Please use this {here} to do something</p>.

instead of {here}.

<h1>weclome here</h1>
<p> Please use this token to do something</p>. {here}

According above the requirement, I come up with two options.

First one, use replace method.

Before use this method, you need to modify your html like this.

<h1> this is emailcontents.html content</h1>
<h1>{token}</h1>

Then use replace method to replace {token}.

app.post("/send", (req, res) => {
    console.log("sending email...");
    const token = req.body.data.token;
    fs.readFile('emailcontents.html', (err, data) => {
        const output = data.toString().replace("{token}", token)
        // do other send mail job
    })

Note: you could use replace only when your html is very very very simple.

If your html is this, it's more complicated to handle it.
In this situation, I'll recommend you use second solution.

<h1>weclome here</h1>
<h1>{token}: {token}</h1>

Second, use template engine

You could use template engine to pass parameter, such as ejs.
It could work in any case.

<!-- emailcontents.ejs -->
<h1>this is emailcontents.html content</h1>
<h1>Please use this <%= token %> to do something</h1>
const ejs = require("ejs")
app.post("/send", (req, res) => {
    console.log("sending email...");
    const token = req.body.data.token;
    ejs.renderFile("./emailcontents.ejs", {token: token}, (error, output) => {
        // output is html string with token which you provide
        // do send mail job
    })
Sign up to request clarification or add additional context in comments.

5 Comments

Comments are not for extended discussion; this conversation has been moved to chat.
Thank you for the thorough explanation. I accepted your answer.
"Note: you could use replace only when your html is very very very simple." What do you mean with very very very simple?
@SJ19 I have an example in my answer. You could image what if your html is <h1>{token}: {token}</h1>, but you wanna replace second {token}? Maybe you could add symbol with + into <h1>{token}: +{token}</h1>, and use replace("+{token}", token). But, every time you changed your html, you might change your string in your replace(string, token) method. It depend on the html content you wanted. You also could image what if you want to replace multiple {token}. That's why I said you could simply use replace method only when your html content is simple without situation I mentioned.
Fair enough. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.