I think you are using that wrongly now. You shouldn't use the dynamic email content like that. It won't be practice solution for you. I used nodemailer to post a email on nodejs. It is one of the most popular packages to post a email.
You can use like this.
mailer.ts:
import nodemailer from "nodemailer";
export default nodemailer.createTransport({
host: "example_host",
port: 2525,
auth: {
user: process.env.MAILTRAP_AUTH_USER,
pass: process.env.MAILTRAP_AUTH_PASSWORD
}
});
compile-email.ts for dynamic html:
import * as path from 'path';
import * as fs from 'fs';
import * as handlebars from 'handlebars';
export default (emailPath: string, replacements: object = {}): string => {
const filePath = path.join(__dirname, `../../views/emails/${emailPath}.html`);
const source = fs.readFileSync(filePath, 'utf-8').toString();
const template = handlebars.compile(source);
return template(replacements);
};
And an example job. Each post email proccess is a job. For example, send-register-code.ts:
import mailer from "../utils/email/mailer";
import compileEmail from "../utils/email/compile-email";
export default async (to: string, replacements: object) => {
await mailer.sendMail({
to,
from: 'keymanager@example.com',
subject: "Register Code",
html: compileEmail('register-code-email', replacements),
});
}
register-code.html:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Register Code: {{code}}
</body>
</html>
And lastly, how can you that job ? For example for using send-register-code job:
import sendRegisterCode from '../../jobs/send-register-code';
sendRegisterCode(email, { code });
I think you will have a structure clearer than now to post email. I hope it would be helpful you