Reputation: 896
I want to use Html file as template in nodejs for sending emails for marketing with some complex level data like graphs populating them dynamically and also show grids etc. I want that to be dynamic like for each user the data can vary. what is the best possible way to do this I want this on server side with data
Upvotes: 3
Views: 3409
Reputation: 166
You can use the email-templates package. The implementation is quite easy. In the case you use nodemailer you'll first need to configure the transport variable just like this (Example with mailtrap):
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'smtp.mailtrap.io',
port: 465,
secure: false,
auth: {
user: // mailtrap.io username
pass: // mailtrap.io password
}
});
Then you use the package creating the email and sending it with the variables you need using the documentation. For example:
const email = new Email({
transport: transporter,
send: true,
preview: false,
views: {
options: {
extension: 'ejs', //or hbs or whatever template you use
},
root: 'path/to/email/templates',
},
});
email.send({
template: 'hello',
message: {
from: 'Daenerys Targaryen <[email protected]>',
to: '[email protected]',
},
locals: {
fname: 'John',
lname: 'Snow',
},
}).then(() => console.log('email has been send!'));
In this case you'll have a file inside email/templates
called hello.ejs
with the variables fname
and lname
(In your case you'll use the user data). For more examples and cases go to the documentation
Upvotes: 1