我在 Next.js 中有一个必须发送电子邮件的项目,它工作正常,但问题是,如果我想检查我要发送的电子邮件是否通过,它会失败。这就是我所拥有的:
await transporter
.sendMail(options)
.then((info) => {
console.log("Email sent:", info.response);
})
.catch((error) => {
console.log("Email not sent:", error);
});
它工作正常,但是当我想返回响应时,它失败了,因为:
error TypeError: Cannot read properties of undefined (reading 'headers')
at eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:266:61)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
我想弄清楚的是如何做到这一点:
await transporter
.sendMail(options)
.then((info) => {
console.log("Email sent:", info.response);
return NextResponse.json({ status: "sent" });
})
.catch((error) => {
console.log("Email not sent:", error);
return NextResponse.json({ status: "error" });
});
我猜错误是由于异步造成的,但我找不到任何其他方法来做到这一点。
由于您的用例是确定电子邮件是否已发送,因此您可以使用
messageId
而不是 response
。
await transporter
.sendMail(options)
.then((info) => {
console.log("Email sent: %s", info.messageId);
})
.catch((error) => {
console.log("Email not sent:", error);
});