小编Mac*_*iej的帖子

Next.JS 和 Nodemailer,从联系表单发送电子邮件

我在 next.js 中的联系表单有问题,我没有任何错误(显示),一切正常,直到部署(在 Vercel 上)。我获取了我的表单,状态为 200,但我没有在 Gmail 上收到我的电子邮件。此外,我没有收到任何其他信息。当我在“开发”和“构建”上测试我的应用程序时,我收到了电子邮件。我在 Gmail 帐户中也有“不太安全的应用”选项。

这是我在 Next.JS 中的代码:

contact.js 中的 fetch 方法:

 fetch("/api/contact", {
        method: "POST",
        headers: {
          Accept: "application/json, text/plain, */*",
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          name: mailName,
          email: mailAddress,
          text: mailText,
        }),
      }).then((res) => {
        console.log("Fetch: ", res);
        res.status === 200
        ?
        router.push("/success")
          : router.push("/error");
Run Code Online (Sandbox Code Playgroud)

在 api/contact.js 中

require("dotenv").config();
const nodemailer = require("nodemailer");

export default (req, res) => {

  const { name, email, text } = req.body;

  const transporter = nodemailer.createTransport({
    service: "gmail", …
Run Code Online (Sandbox Code Playgroud)

javascript gmail node.js nodemailer next.js

8
推荐指数
1
解决办法
6109
查看次数

标签 统计

gmail ×1

javascript ×1

next.js ×1

node.js ×1

nodemailer ×1