从Nodejs发送的错误请求-原始消息缺少头部信息



我正在尝试在vercel主机上使用nodejs发送电子邮件。

我正在使用sendgrid/mail api收到一个错误的请求。它看起来好像没有在Sendgrid请求中创建头信息。

当我转储消息时,它只是我发送的json信息吗?

这是我正在使用的代码。

const sgMail = require('@sendgrid/mail')
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY;
export default function (req, res) {
const mailData = {
from: 'dhook@fullonconsulting.com',
to: req.body.email,
subject: 'Message to Full On Consulting',
text: req.body.message,
html: '<div>'+req.body.message+'</div>'
}
const {
classes: {
Mail,
},
} = require('@sendgrid/helpers');
const mail = Mail.create(mailData);
const body = mail.toJSON();
console.log("======================================== ");
console.log("RAW BODY: " + JSON.stringify(body));
console.log("======================================== ");
sendMail(mailData)
.then((result) => {
console.log('Email sent...', result);
res.status(200).json({ status: 'SUCCESS' })
})
.catch((error) => console.log('Error ... ' + error.message));
}
async function sendMail(mailData) {
try {
var promise = new Promise( (resolve, reject) => {
sgMail.setApiKey(SENDGRID_API_KEY)
const msg = {
to: mailData.to, // Change to your recipient
from: mailData.from, // Change to your verified sender
subject: mailData.subject,
text: mailData.text,
html: mailData.html,
}
console.log("API KEY: " + SENDGRID_API_KEY);
console.log(JSON.stringify(msg));
sgMail
.send(msg)
.then(() => {
console.log('Email sent');
})
.catch((error) => {
console.error(error);
console.log('RECEIVED ERROR')
})
});
//promise.then( result => {
// console.log("PRomise Success ...");
//}, function(error) {
// console.log("Promise Failure...");
//});
} catch (error) {
console.log("CATCH ERROR: " + error)
return error;
}
}

这里是错误

ResponseError: Bad Request
at node_modules/@sendgrid/client/src/classes/client.js:146:29
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 400,
response: {
headers: {
server: 'nginx',
date: 'Sat, 30 Oct 2021 13:51:20 GMT',
'content-type': 'application/json',
'content-length': '219',
connection: 'close',
'access-control-allow-origin': 'https://sendgrid.api-docs.io',
'access-control-allow-methods': 'POST',
'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
'access-control-max-age': '600',
'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html',
'strict-transport-security': 'max-age=600; includeSubDomains'
},
body: { errors: [Array] }
}
}

我没有正确地从前端表单传递信息,这导致了这个问题。

您可以尝试展开response.body对象并查找您正在获得的帮助消息和错误消息。

步骤1:您应该验证sendgrid帐户设置->发件人身份验证->验证单个发件人身份验证

步骤2:sendgrid From Email: "verifiedemail@gmail.com">

步骤3:发送电子邮件&&Env文件配置

相关内容

  • 没有找到相关文章

最新更新