0

I have a NodeJS script utilizing Nodemailer to send an email with the Linux system information of the device the script is running on once a button is pressed in my application.

Below is a shortened excerpt from the script:

const nodemailer = require('nodemailer');
...
...
 // Message object
    let message = {
        // Comma separated list of recipients
        to: 'Nodemailer <[email protected]>',

        // Subject of the message
        subject: 'Test message ' + Date.now(),

        // HTML body
        html: `<p><b>Hello</b>, below is some sample system information</p>
        <p>{{ip-address}} {{os-version}}<br/></p>`,

How can I replace the example text {{ip-address}} and {{os-version}} with values from a variable?

Note: One thought I had was perhaps to save the value of a shell command executed by cp.exec to a variable, but my obstacle remains how to pass the contents of that variable to the placeholder text.

The information saved to {{ip-address}} and {{os-version}} isn't static so I can't just set permanent .ENV variables and call it day, I need to get fresh information every time this script is run.

4
  • I don't see {{ip-address}} or {{os-version}} getting any value assigned to them. Where are they being declared and assigned a value? Commented Jan 8, 2020 at 18:28
  • @abhinav Hello, the question itself is asking how to do just that. {{ip-address}} and {{os-version}} are verbatim text in the body of the email that need to be replaced. Any thoughts? Commented Jan 8, 2020 at 18:30
  • Man, I think you need to rephrase the question to How to get the IP address and OS version of the system in Nodejs or something similar. From the looks of it, it seems like you are asking a way to replace the variable with its value in a string. Commented Jan 8, 2020 at 18:34
  • @abhinav IP address and OS version are just examples for the purpose of the question, but yes my question is essentially how to replace a value in a long string with the value of a variable. Once I figure that out with the help of the community I can use it as a template to apply to a variety of placeholder values. Commented Jan 8, 2020 at 18:37

1 Answer 1

3

Since you're using ` notation you can do

html: `<p><b>Hello</b>, below is some sample system information</p>
    <p>${ip-address} ${os-version}<br/></p>`

Javascript inside of the ${} notation will be evaluated and inserted in the string.

Template strings documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.