流星JS:“邮件未发送;要启用发送,请设置MAIL_URL环境变量”

时间:2018-07-11 06:36:27

标签: meteor

我收到以下邮件错误:

“未发送邮件;要启用发送,请设置MAIL_URL环境变量。”

不知道为什么。请帮忙。我仔细检查过,邮件服务器正常。在流星站点上也尝试过一切: https://docs.meteor.com/environment-variables.html#MAIL-URL

但仍然出现错误。

我的代码在服务器文件夹中,这是config.js文件:

AWS.config.update({
    accessKeyId: Meteor.settings.private.s3Settings.awsAccessKeyId,
    secretAccessKey: Meteor.settings.private.s3Settings.awsSecretKey,
});
STS = new AWS.STS();

S3 = new AWS.S3();

Mailer.config({
    from: 'export@INSIDEDOMAIN.com',
    replyTo: 'export@INSIDEDOMAIN.com',
    addRoutes: false
});

Meteor.startup(() => {
    Mailer.init({
        templates: Templates
    });

    if (Meteor.isProduction) {
        process.env.MAIL_URL = 'smtp://export%40INSIDEDOMAIN.com:INSIDEPASS@mail.INSIDEDOMAIN.com:465/';
    }

    Meteor.call('check_users_trial', function(err) {
        if (err) {
            console.log(err);
        }
    });
});

SyncedCron.start();

Meteor.methods({
    check_users_trial: function() {
        function checkSubscriptions() {
            const fromDate = new Date(new Date().setDate(new Date().getDate() - 15)),
                toDate = new Date(new Date().setDate(new Date().getDate() - 13));

            const users = Accounts.users.find({
                'profile.isSubscribed': false,
                'profile.warningEmailSent': false,
                createdAt: {
                    $gt: fromDate,
                    $lt: toDate
                },
                roles: {
                    $ne: 'admin'
                }
            });

            users.forEach(function(user) {
                if (!user.profile.isSubscribed) {
                    let isSubscriptionValid = false;
                    const today = new Date();
                    const registerDate = new Date(user.createdAt);
                    const checkDate = registerDate.setDate(registerDate.getDate() + 14);

                    if (checkDate > today.getTime()) {
                        isSubscriptionValid = true;
                    }

                    if (!isSubscriptionValid) {
                        Meteor.call('send_warining_email_trial', user.emails[0].address, function(error) {
                            if (error) {
                                console.log(error);
                            } else {
                                Accounts.users.update({
                                    _id: user._id
                                }, {
                                    $set: {
                                        'profile.warningEmailSent': true
                                    }
                                });
                            }
                        });
                    }
                }
            });
        }

        SyncedCron.add({
            name: 'Send emails to users, whose trial period ended.',
            schedule: function(parser) {
                return parser.text('every 1 hours');
            },
            job: function() {
                checkSubscriptions();
            }
        });
    }
});

1 个答案:

答案 0 :(得分:3)

process.env.MAIL_URL仅在startup()完成后设置,为时已晚。
移动以下几行

if (Meteor.isProduction) {
    process.env.MAIL_URL = 'smtp://export%40INSIDEDOMAIN.com:INSIDEPASS@mail.INSIDEDOMAIN.com:465/';
}

在您的Meteor.startup通话上方。并确保isProduction实际上是true,否则该代码将不会执行。

相关问题