Reputation: 478
Currently im using sendgrid using nodejs library “('nodemailer')” TransporterOption:
config:
transporterOption: {
host: process.env.SMTP_HOST, // smtp.sendgrid.net
pool: true,
maxConnections: 20,
rateDelta: 1000,
rateLimit: 150,
auth: {
user: process.env.USER_NAME,
pass: process.env.PASSWORD
}
},
sending code :
const config = require('./config');
const nodeMailer = require('nodemailer');
const htmlToText = require('nodemailer-html-to-text').htmlToText;
const transporter = nodeMailer.createTransport(config.transporterOption);
transporter.use('compile', htmlToText());
try {
let mailOptions = {
headers: { 'X-SMTPAPI': `{"category":["${myid}"]}` },
from: `"myname" <${toemail}>`,
replyTo: `"myname" <${toemail}>`,
to: `"name" <${emailAdr}>`,
subject: mysubject,
text: mytextbody,
html: myhtmlbodymsg
}
} catch (e) {
console.log(e);
}
migrating from username/password to API key when passing api_key in response getting this error "Mail command failed: 550 Unauthenticated senders not allowed”
option 1:
transporterOption: {
host: process.env.SMTP_HOST, // smtp.sendgrid.net
pool: true,
maxConnections: 20,
rateDelta: 1000,
rateLimit: 150,
auth: {
api_key: process.env.SENDGRID_API_KEY // fakeKEY.kashdkjhjkdhsakjdhksajhd
}
},
"err":{"message":"Missing credentials for "PLAIN"","name":"Error","stack":"Error: Missing credentials for "PLAIN"\n
Option 2 :
transporterOption: {
host: process.env.SMTP_HOST, // smtp.sendgrid.net
pool: true,
maxConnections: 20,
rateDelta: 1000,
rateLimit: 150,
auth: {
Username: process.env.USER_NAME, //’apikey’
Password: process.env.SENDGRID_API_KEY // fakekeyssssjhkjsahdkjsahdkjhsa
}
},
Error : “{"message":"Mail command failed: 550 Unauthenticated senders not allowed","name":"Error","stack":"Error: Mail command failed: 550 Unauthenticated senders not allowed\n”
Option 3 :
transporterOption: {
host: process.env.SMTP_HOST, // smtp.sendgrid.net
pool: true,
maxConnections: 20,
rateDelta: 1000,
rateLimit: 150,
auth: {
api_user: 'apikey',
api_key: process.env.SENDGRID_API_KEY // fakekeyssssjhkjsahdkjsahdkjhsa
}
},
Error : “{"message":"Mail command failed: 550 Unauthenticated senders not allowed","name":"Error","stack":"Error: Mail command failed: 550 Unauthenticated senders not allowed\n”
Upvotes: 4
Views: 3680
Reputation: 1824
You can install the nodemailer-sendgrid package and then create a transport like this:
const nodemailer = require('nodemailer');
const nodemailerSendgrid = require('nodemailer-sendgrid');
const transport = nodemailer.createTransport(
nodemailerSendgrid({
apiKey: process.env.SENDGRID_API_KEY
})
);
If you are using TypeScript, you can also install the corresponding types with npm install --save @types/nodemailer-sendgrid
.
It seems that this has replaced the outdated nodemailer-sendgrid-transport and has been written by the author of nodemailer. One small downside is that it's using the vendor-specific library under the hood.
Upvotes: 4
Reputation: 864
I also needed to migrate from basic authentication to API keys for Sendgrid.
I tried to use the API keys in the following way and probably works but do not know if we are still using basic authentication by this way which will soon fail due to compulsory
2FA.
Let me know if what we are doing here is correct.
// This below auth object is specific to NODEMAILER configuration options
// This is NOT the same for sendgrid native API configuration
// you can try with your keys named Username/Password but mine works with user/pass
auth: {
'user': 'apikey', // <--- keep as is
'pass': 'SG.aslknwefn_RANDOM_FAKE_KEY', // <--- your api key
}
sendgrid reference links:
https://sendgrid.com/docs/for-developers/sending-email/upgrade-your-authentication-method-to-api-keys
https://sendgrid.com/docs/for-developers/sending-email/authentication/
Upvotes: 8