Hope everyone doing great.
Actually, I'm trying to build an email automation tool where a user can send an email to multiple recipients and I want to implement such a functionality where the user can write email body in a text file e.g Hello ${name}, how are you ${name} and the program simply read body from this text file and replace ${name} with the names of the recipients that comes from .csv file.
Problem: The problem I face here is instead of replacing ${name} with names it simply sends the exact text read from the file for example Instead of Hello John, How are you it sends Hello ${name}, How are you. So, Is there any way to convert plain text into JS template literal or any other way to do this task? It would be a great help.
Thanks in Advance
You can try vm module:
import vm from 'vm';
const context = {
name: 'Jane',
};
vm.createContext(context);
const templateFileContent = 'Hello ${name}, how are you ${name}';
const interpolatedString = vm.runInContext(`\`${templateFileContent}\``, context);
console.log(interpolatedString);