永远使用时,从节点服务器发送电子邮件失败

时间:2022-10-23 18:18:14

I have an API server running with Node and the following add user function:

我有一个运行Node的API服务器和以下添加用户功能:

add: function (userArray, onSuccess, onError) {
userArray.forEach(function (user) {
    var length = 8,
        charset = "abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
        retVal = "";
    for (var i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }

    var password = retVal;
    var salt = bcrypt.genSaltSync(10);
    var password = bcrypt.hashSync(password, salt);
    user.user_type_id = user.user_type.id;
    if (user.title) {
        user.title_id = user.title.id;
    }
    user.division_id = user.division.id;
    user.password = password;
    user.user_password = retVal;
    user.image_path = 'img/AdamProfil.png';
});

emailTemplates(templatesDir, function (err, template) {
    if (err) {
        console.log(err);
    } else {
        var transportBatch = nodemailer.createTransport(smtpTransport({
            host: 'smtp.mail.dk',
            secureConnection: true,
            port: 587,
            tls: {
                rejectUnauthorized: false
            },
            auth: {
                user: 'my@mail.com',
                pass: 'myPassword'
            }
        }));

        var Render = function(locals) {
            this.locals = locals;
            this.send = function(err, html, text) {
                if (err) {
                    console.log(err);
                } else {
                    transportBatch.sendMail({
                        from: '*** <support@mymail.dk>',
                        to: locals.username,
                        subject: 'Din adgangskode til ***!',
                        html: html,
                        // generateTextFromHTML: true,
                        text: text
                    }, function(err, responseStatus) {
                        if (err) {
                            console.log(err);
                        } else {
                            console.log(responseStatus.message);
                        }
                    });
                }
            };
            this.batch = function(batch) {
                batch(this.locals, templatesDir, this.send);
            };
        };

        // Send multiple emails
        template('password', true, function(err, batch) {
            for(var user in userArray) {
                var render = new Render(userArray[user]);
                render.batch(batch);
            }
        });
    }
});

userArray.forEach(function(user){
    User.create(user)
        .then(function(createdUser) {
            user.profile.user_id = createdUser[null];

            Profile.create(user.profile);
        });
    });
onSuccess(userArray);
}

Now when i run my server.js file using the console writing node server.js and run this function it correctly sends an email to the target (SUCCESS!?) no because when i run this with forever start server.js and run the exact same function with the exact same parameters no email is sent.

现在,当我使用控制台编写节点server.js运行我的server.js文件并运行此函数时,它正确地向目标发送一封电子邮件(SUCCESS!?)否,因为当我运行它时永远启动server.js并运行确切的具有完全相同参数的相同功能不发送电子邮件。

What the hell is going on?:)

这到底是怎么回事?:)

1 个解决方案

#1


Looking at your code only, I see a templatesDir variable. I'd guess that is different, or changing in an unexpected way, when you run with forever.

仅查看代码,我看到了templatesDir变量。当你永远奔跑时,我猜这是不同的,或以一种意想不到的方式改变。

Check out this issue at forever. Do you ever reference an absolute path, or process.cwd()?

永远查看这个问题。你有没有引用绝对路径,或者是process.cwd()?

Worth a try, good luck!

值得一试,祝你好运!

#1


Looking at your code only, I see a templatesDir variable. I'd guess that is different, or changing in an unexpected way, when you run with forever.

仅查看代码,我看到了templatesDir变量。当你永远奔跑时,我猜这是不同的,或以一种意想不到的方式改变。

Check out this issue at forever. Do you ever reference an absolute path, or process.cwd()?

永远查看这个问题。你有没有引用绝对路径,或者是process.cwd()?

Worth a try, good luck!

值得一试,祝你好运!