Next.js API路由抛出错误nodemailer中的内部服务器错误

vql8enpb  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(128)

我使用cpanel来托管我的下一个js网站,我有这个API路由,使用nodemailer发送消息,任何时候我调用路由,我得到内部服务器错误,但它在localhost甚至vercel enter image description here上工作正常
这里是API route => /api/mail

import { NextApiRequest, NextApiResponse } from "next";
import nodemailer from "nodemailer";

type Data = {
  message: string;
};

const handler = async (req: NextApiRequest, res: NextApiResponse<Data>) => {
  const message = {
    from: req.body.email,
    to: "enquiries@domain.com.ng",
    subject: "New Customer Setup",
    text: `${req.body.email} ${req.body.firstname} ${req.body.lastname}`,
    html: `
    <p>Name: ${req.body.firstName} ${req.body.lastName}</p>
    <p>Email: ${req.body.email}</p>
    <p>Phone Number: ${req.body.mobileNumber}</p>
    <p>Residential Address: ${req.body.residentialAddress}</p>
    <p>Gender: ${req.body.gender?.value}</p>
    `,
  };

  const transporter = nodemailer.createTransport({
    secure: true,
    port: 465,
    host: "domain.com.ng",
    auth: {
      user: "enquiries@domain.com.ng",
      pass: "pass",
    },
  });

  try {
    const info = await transporter.sendMail(message);
    console.log(info);
    res.status(200).json({
      message:
        "Thank you for your interest in domain. Our relationship agent will get back to you shortly.",
    });
  } catch (error:any) {
    console.error("Error sending email:", error);
    
    if (error.code === "ECONNREFUSED") {
      res.status(500).json({ message: "Failed to connect to the email server." });
    } else if (error.code === "EAUTH") {
      res.status(500).json({ message: "Invalid email credentials. Authentication failed." });
    } else {
      res.status(500).json({ message: "An error occurred while sending the email." });
    }
  }
};

export default handler;

则调用路由的函数=>

const onSubmit = async (data: FormData) => {
    const res = await fetch('/api/mail', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'accept': 'application/json , text/plain, */*'
      },
      body: JSON.stringify(data)

    })
    console.log(res, 'response from server')
    if (res.status === 200) {
      const json = await res.json()
      toast.success(json.message)
      reset()
      router.push('/')
    }
  }

但是每当我像https://domain.com.ng/api/mail一样访问API路由时,我都会得到发送邮件时出错
如果我填写表单发送,我得到内部服务器错误我希望它记录状态确定并发送消息。
所以最后,我已经能够停止得到内部服务器错误作为使用异步等待的结果,但仍然,收件人没有收到邮件,即使它返回成功。

我的服务器日志

  • App 9469输出:at TLSSocket.SMTPConnection._onSocketError(/home2/funditco/nodevenv/frontend_test/16/lib/node_modules/nodemailer/lib/smtp-connection/index.js:194:45)App 9469输出:at SMTPConnection._onError(/home2/funditco/nodevenv/frontend_test/16/lib/node_modules/nodemailer/lib/smtp-connection/index.js:780:14)App 9469输出:在SMTPConnection。(/home2/funditco/nodevenv/frontend_test/16/lib/node_modules/nodemailer/lib/smtp-transport/index.js:176:24)App 9469输出:在/home2/funditco/nodevenv/frontend_test/16/lib/node_modules/nodemailer/lib/mailer/index.js:230:21
xurqigkl

xurqigkl1#

它现在正在工作,所以我得到内部服务器错误的原因是主机名的结果,我的主机提供商已经设置了SMTP配置。我所做的只是将主机名更改为‘localhost’

相关问题