Mike
Mike

Reputation: 4259

Nodemailer / Sendgrid with apikey

I am trying to get a simple contact form up and running in node.js with sendgrid using an apikey. It's unclear where I am going wrong.

I tried the following: 1.

var options = {
    auth: {
        api_user: 'xjh-4XqZGH6$HLT-IEOPFG',
        api_key: 'SG.xjh-4XqZGH6$HLT-IEOPFG.Wl2rB-CN00-nj3x5NiKno7MpDk8DxTtvgJeZfDGGI' // fake obviously, but I would like to show the structure as I am not sure whether this is correct
    }
};

and 2.

var options = {
    auth: {
        api_user: 'xjh-4XqZGH6$HLT-IEOPFG',
        api_key: 'Wl2rB-CN00-nj3x5NiKno7MpDk8DxTtvgJeZfDGGI' // fake obviously, but I would like to show the structure as I am not sure whether this is correct
    }
};

and 3.

var options = {
    auth: {
        api_user: 'apikey',
        api_key: 'SG.xnh-4XqZGH6$HLT-IEOPFG.Wl2rB-CN00-nj3x5NiKno7MpDk8DxTtvgJeZfDGGI' // fake obviously, but I would like to show the structure as I am not sure whether this is correct
    }
};

It keeps coming back with badusername / password.

It works fine with my username (api_user) and password (api_key).

Can anyone explain what I need to do?

Cheers, Mike

Upvotes: 6

Views: 10395

Answers (3)

Branislav
Branislav

Reputation: 130

You can use SMTP and API key together:

const mailer = nodemailer.createTransport({
    host: 'smtp.sendgrid.net',
    port: 465,
    secure: true,
    auth: {
        user: 'apikey',
        pass: 'SG.XXXX.XXXX',
    },
});

Official docs: : https://sendgrid.com/docs/API_Reference/SMTP_API/integrating_with_the_smtp_api.html

Upvotes: 8

Klemens Zleptnig
Klemens Zleptnig

Reputation: 1824

For anybody stumbling over this: The preferred way now seems to be to install the nodemailer-sendgrid package which allows you to set an apiKey property - see my answer here https://stackoverflow.com/a/64726992/769726

Upvotes: 1

Mike
Mike

Reputation: 4259

OK, I figured it out. You remove the api_user in this case and use the long key. E.g.

var options = {
auth: {
    api_key: 'SG.xnh-4XqZGH6$HLT-IEOPFG.Wl2rB-CN00-nj3x5NiKno7MpDk8DxTtvgJeZfDGGI' // fake
}

};

Best, Mike

Upvotes: 21

Related Questions