node.js - How to send a ejs file as HTML email from nodejs application as cronjob -
i have node.js app , want send html email application daily @ 8am few people. have dashboard.ejs file gets data database , shows them in bootstrap dashboard template. want send dashboard.ejs html email datas database , styling same when dashboard.ejs loaded application.
i have cron job ready , mailer function ready. unable send dashboard.ejs file html via email.can done? if yes, appreciated.
var mailer = require('express-mailer'); var cronjob = require('cron').cronjob; mailer.extend(app, { from: 'user@gmailcom', host: 'smtp.gmail.com', secureconnection: true; port: 465, // port secure smtp transportmethod: 'smtp', auth: { user: 'user@gmail.com', pass: 'password' } }); var job = new cronjob('00 49 * * * *', function() { console.log('this runs on 49th second of every min every hour every day every month every year') app.mailer.send('dashboards/dashboard', { to: 'user2@gmail.com', subject: 'test email', message:'hello world' }, function(err) { if(err) console.log('error', err); } ); }, function() { console.log('this function executed when job stops'); }, false, 'asia/kolkata' ); job.start();
unless you're building existing express app discourage approach. you're not using express in above example more view engines.
instead of using express, take part need , use it. can use ejs
directly generate html data using ejs.renderfile()
. use nodemailer
send e-mail (which express-mail
uses send e-mails you).
const ejs = require('ejs'); const nodemailer = require('nodemailer'); const cronjob = require('cron').cronjob; const template = './dashboards/dashboard.ejs'; const transport = nodemailer.createtransport({ host: 'smtp.gmail.com', port: 465, secure: true, auth: { user: 'user@gmail.com', pass: '*' } }); const job = new cronjob('00 49 * * * *', function() { let templatedata = { name: 'test name' }; ejs.renderfile(template, templatedata, (err, html) => { if (err) console.log(err); // handle error console.log(`html: ${html}`); let mailopts = { from: 'user@gmail.com', to: 'recipient', subject: 'ejs test file', html: html }; transport.sendmail(mailopts, (err, info) => { if (err) console.log(err); //handle error console.log(info); }); }); }, () => { console.log('this function executed when job stops'); }, false, 'asia/kolkata' }; job.start();
Comments
Post a Comment