【问题标题】:Schedule Email using Agenda使用议程安排电子邮件
【发布时间】:2017-02-02 19:36:15
【问题描述】:

我正在尝试安排使用 NodeMailer 和 Agenda 发送电子邮件,但是,我没有看到 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...');

当我使用node index.js 运行上述代码时,我看到以下控制台消息

Wait 10 seconds...

基本上,我正在尝试安排电子邮件功能每 5 秒运行一次。

我哪里做错了?

编辑:这似乎与工作实施相比:

 agenda.every('5 seconds', jobName);

尽管事后我需要作业对象,所以如果有办法通过运行上述命令来获得它,那将是一个可以接受的答案。

【问题讨论】:

    标签: javascript node.js gmail nodemailer agenda


    【解决方案1】:

    Agenda 在成功建立它的 mongo 连接时调用 'ready' 事件。我没有看到你设置它。比如:

     var Agenda = require('agenda');
     var mongoConnectionString = "mongodb://127.0.0.1/agenda";
     var agenda = new Agenda({db: {address: mongoConnectionString}});
    

    【讨论】:

    • 这里我不是指mongo,只是smtp server
    • 忽略我的其他评论,我以为我的问题完全不同。议程依赖于 mongo。这是它存储所有工作的地方。因此,如果您不设置 mongo,它们就不会运行。
    猜你喜欢
    • 2020-02-23
    • 2017-04-05
    • 1970-01-01
    • 2012-09-11
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    相关资源
    最近更新 更多