0

I'm trying to call a callback function on my SendMail with nodemailer. Although the function is successfully executed and the mail is sent, the callback function logging "mail sent" on the console is never executed. Any ideas what I'm missing?

var email = '[email protected]'
async.waterfall([
    function(done) {
        crypto.randomBytes(20, function(err, buf) {
          var token = buf.toString('hex');
          return done(err, token);
        });
    },
     function(token, done){
        //some code
        done(null,token);
    },
    function(token, done){
        if(token){
            var mailOptions = {
                to: email,
                from: '[email protected]',
                subject: 'subject',
                text: 'text'
                };
            mailer.SendMail(mailOptions, function(err) {
                if(err){
                    console.log(err);
                }else {
                    console.log("no error");
                }
                console.log("sent mail");
                req.flash('info', 'An e-mail has been sent to ' + email + ' with further instructions.');
                console.log("mail sent");
                done(err, 'done');
            });
        }
        else return done("Account existiert nicht", null);
    }  
],
    function(err){
        console.log("final block");
        if(err){
            console.log(err);
            return res.status(500).send({msg:"error"});
        }
        else {
            return res.status(200).send({msg: "Reset Link has been sent to you."})
        }
    }
);

This is the corresponding SendMail() function:

const nodemailer = require("nodemailer");

exports.SendMail = (msg) => {
    let transporter = nodemailer.createTransport({
        service: 'gmail',
        host: "smtp.gmail.com",
        secure: true,
        auth: {
            user: "[email protected]", // my mail
            pass: "mysecretpwd" // my password
        }
      });

    return transporter.sendMail(msg, function (error, info) {
        if(error) {
            return console.log(error);
        }
        console.log("Message sent "+ info.response) // this statement is showing on the console
      });
}

1 Answer 1

1

Your function does not use the second parameter (the callback). You can use something like:

const nodemailer = require("nodemailer");

exports.SendMail = (msg, callback) => {
    let transporter = nodemailer.createTransport({
        service: 'gmail',
        host: "smtp.gmail.com",
        secure: true,
        auth: {
            user: "[email protected]", // my mail
            pass: "mysecretpwd" // my password
        }
      });

    return transporter.sendMail(msg, callback);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.