Nodejs Nodemailer : Sendgrid SMTP username & password to API key using existing code error : 550
Nodejs Nodemailer : Sendgrid SMTP username & password to API key using existing code error : 550
目前我正在使用 nodejs 库“('nodemailer')”使用 sendgrid
转运选项:
配置:
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
}
},
发送代码:
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);
}
工作正常,没有任何问题。
从 username/password 迁移到 API 密钥
当通过 api_key 响应时收到此错误“邮件命令失败:不允许 550 未经身份验证的发件人”
选项 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
选项 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”
选项 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”
我还需要从基本身份验证迁移到 Sendgrid 的 API 密钥。
我尝试按以下方式使用 API 密钥并且可能有效但不知道我们是否仍在通过这种方式使用基本身份验证,由于强制性的原因很快就会失败
2FA.
让我知道我们在这里做的是否正确。
// 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 参考链接:
https://sendgrid.com/docs/for-developers/sending-email/upgrade-your-authentication-method-to-api-keys
https://sendgrid.com/docs/for-developers/sending-email/authentication/
您可以安装 nodemailer-sendgrid 包,然后像这样创建传输:
const nodemailer = require('nodemailer');
const nodemailerSendgrid = require('nodemailer-sendgrid');
const transport = nodemailer.createTransport(
nodemailerSendgrid({
apiKey: process.env.SENDGRID_API_KEY
})
);
如果你使用的是TypeScript,你也可以通过npm install --save @types/nodemailer-sendgrid
安装相应的类型。
看来这已经取代了过时的nodemailer-sendgrid-transport and has been written by the author of nodemailer。一个小缺点是它在后台使用 vendor-specific 库。
目前我正在使用 nodejs 库“('nodemailer')”使用 sendgrid 转运选项:
配置:
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
}
},
发送代码:
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);
}
工作正常,没有任何问题。
从 username/password 迁移到 API 密钥 当通过 api_key 响应时收到此错误“邮件命令失败:不允许 550 未经身份验证的发件人”
选项 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
选项 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”
选项 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”
我还需要从基本身份验证迁移到 Sendgrid 的 API 密钥。
我尝试按以下方式使用 API 密钥并且可能有效但不知道我们是否仍在通过这种方式使用基本身份验证,由于强制性的原因很快就会失败
2FA.
让我知道我们在这里做的是否正确。
// 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 参考链接:
https://sendgrid.com/docs/for-developers/sending-email/upgrade-your-authentication-method-to-api-keys
https://sendgrid.com/docs/for-developers/sending-email/authentication/
您可以安装 nodemailer-sendgrid 包,然后像这样创建传输:
const nodemailer = require('nodemailer');
const nodemailerSendgrid = require('nodemailer-sendgrid');
const transport = nodemailer.createTransport(
nodemailerSendgrid({
apiKey: process.env.SENDGRID_API_KEY
})
);
如果你使用的是TypeScript,你也可以通过npm install --save @types/nodemailer-sendgrid
安装相应的类型。
看来这已经取代了过时的nodemailer-sendgrid-transport and has been written by the author of nodemailer。一个小缺点是它在后台使用 vendor-specific 库。