1
votes

So, I'm using cloud9 as the IDE, nodejs, express and [email protected] and nodemailer-sendgrid-transport. Here's my code:

var nodemailer=require('nodemailer');
var sgTransporter = require('nodemailer-sendgrid-transport');
var options={
    auth: {
        api_user: "xxx",
        api_pass: "xxx"
    }
};
var client = nodemailer.createTransport(sgTransporter(options));

var sendMail=function(emailTo, subject, html, fn){
    var mailOptions={
        from:'xxx',
        to:emailTo,
        subject:subject,
        html:html
    };

    client.sendMail(mailOptions, function(error, response){
        if(error){
            console.log(error);
            fn(error);
        }
        else{
            console.log("Message sent: " + response.message);
            fn(response);
        }
    });
};

And here the sendMail function gets called:

router.get('/verifyEmail', function(req,res){
    if(req.isAuthenticated()) {
        var subject='Verify your email address';
        var link="https://myapp-myuser.c9users.io/verify/:"+req.user._id;
        var html='Hello,<br> Please Click on the link to verify your Account.<br><a href='+link+'>Click here to verify</a>';
        console.log('Sending verification email to: '+req.user.email);
        sendMail(req.user.email,subject, html,function(error, response){
            if (error){
                return res.status(400).json({error:error});
            }
            return res.status(200).json(response);
        });
    }
});

But the email is not being sent... I get the following error: [Error: No transport method defined]

I have tried many different solutions. The way I see it, I'm doing exactly the same thing as in this example: https://sendgrid.com/blog/sending-email-nodemailer-sendgrid/ Is this perhaps a cloud9 issue?? I have tried with a gmail account as well. Same error, after allowing unsafe apps...

EDIT: Ok, I've upgraded nodemailer to version 2.4.2 and a new error gets thrown... [Error: The provided authorization grant is invalid, expired, or revoked]

1

1 Answers

0
votes

As most of the times... it's the little things, gosh I was stuck in this... Problem was this:

auth: {
    api_user: "xxx",
    api_pass: "xxx"
}

Should have been written like this:

auth: {
    api_user: "xxx",
    api_key: "xxx"
}

That, and nodemailer's version...