I am trying to schedule to sent out an email using NodeMailer and Agenda, however, I don't see anything happening with the below code in index.js
:
var Agenda = require('agenda');
var agenda = new Agenda();
const nodemailer = require('nodemailer');
agenda.define('send email', {priority: 'high', concurrency: 10}, function(job, done) {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'xxxx@gmail.com',
pass: 'xxxx'
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Test" <xxxx@gmail.com>', // sender address
to: 'xxxx@gmail.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ?', // plain text body
html: '<b>Hello world ?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, response) {
console.log('Message sent: ' + response.message);
transporter.close();
done();
});
});
agenda.on('ready', function() {
agenda.every('5 seconds', 'send email');
agenda.start();
});
agenda.on('start', function (job) {
console.log("Job %s starting", job.attrs.name);
});
agenda.on('complete', function (job) {
console.log("Job %s finished", job.attrs.name);
});
console.log('Wait 10 seconds...');
When I run the above code using node index.js
, i see the following console message
Wait 10 seconds...
Basically, I am trying to schedule the email functionality to run for every 5 secs.
Where am I doing wrong?
EDIT: It seems to be with the job implementation versus:
agenda.every('5 seconds', jobName);
I need the job object after the fact though, so if there is a way to get that from running the above command, that would be an acceptable answer.