Reputation: 16172
I have created an email template, using the pug engine. The template is inside the views
folder and the name is index.pug
.
I want to dynamically add the data to the template. The data is present in the users
array. I used this object in the template using for each
method. Viewing the template locally, I am able to see the dynamic content in the template.
But when the same template is sent using nodemailer, the dynamic content is not visible there.
Below is the code of index.js
file
const express = require('express'); // Import the express dependency
const app = express();
const path = require('path'); // Instantiate an express app, the main work horse of this server
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'pug');
const users = [
{
"id" : 1,
"pncde" : "PB001-3W",
"desc":"Face Mask, White (50 pcs/box)",
"qty":"1",
"unitPrice":"$3.00",
"serviceFee":"$3.00",
"amount":"$4.00"
},
{
"id" : 2,
"pncde" : "PB001-3W",
"desc":"Face Mask, White (50 pcs/box)",
"qty":"1",
"unitPrice":"$3.00",
"serviceFee":"$3.00",
"amount":"$4.00"
}
]
app.get('/', (req, res) => {
res.render('index',{ 'users': users });
})
app.listen(3000, () => console.log('Listening on port 3000'))
var nodemailer = require('nodemailer');
const {pugEngine} = require("nodemailer-pug-engine");
const mailer = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'pwtiuyhjtn'
}
});
mailer.use('compile', pugEngine({
templateDir: __dirname + '/views',
pretty: true
}));
mailer.sendMail({
to: '[email protected]',
template: 'index',
subject: "Purchase Order Template for 10 products",
ctx: {
}
});
Can anyone please help me resolve this issue ?
Upvotes: 0
Views: 686
Reputation: 5704
You are not passing the users
to the template using ctx
object.
mailer.sendMail({
to: '[email protected]',
template: 'index',
subject: "Purchase Order Template for 10 products",
ctx: {
users // <-- same as res.render('index',{ users });
}
});
By the way, this { users }
is shortcut for yours { 'users': users }
just to be clear.
Upvotes: 2