You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Next.js API路由中使用Resend发送邮件时出现500错误求助

Next.js + Resend 邮件发送500错误排查求助

我在构建Next.js应用时,尝试在API路由中使用Resend服务发送邮件,但持续收到500错误,响应内容为{"error":{}}。怀疑是resend.emails.send方法的使用问题,但无法进一步排查,以下是详细信息:

相关代码

app/api/send/route.ts

import { Resend } from "resend";
import { MeetingRequest } from "@/api/email-templates/MeetingRequest";

const resend = new Resend(process.env.NEXT_PUBLIC_RESEND_KEY);

export async function POST(request: Request) {
  const {
    to_email,
    receiver_name,
    scheduler_id,
    scheduler_name,
    scheduled_time,
    scheduler_tz,
    question_title,
    question_id,
    sender_note,
  } = await request.json();

  try {
    const { data, error } = await resend.emails.send({
      from: "Codey from CodingOH <codey@codingoh.com>",
      to: [to_email],
      subject: `You received a meeting request from ${scheduler_name}!`,
      react: MeetingRequest({
        receiver_name,
        scheduler_id,
        scheduler_name,
        scheduled_time,
        scheduler_tz,
        question_title,
        question_id,
        sender_note,
      }),
    });

    if (error) {
      console.error("[UP] Error sending email:", error);
      return Response.json({ error }, { status: 500 });
    }

    return Response.json({ data });
  } catch (error) {
    console.error("[DOWN] Error sending email:", error);
    return Response.json({ error }, { status: 500 });
  }
}

app/questions/[id]/route.tsx

const response = await fetch("/api/send", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    to_email: question.asker?.email_address ?? "",
    receiver_name: question.asker?.first_name ?? "",
    scheduler_id: coder?.auth_id ?? "",
    scheduler_name: `${coder?.first_name} ${coder?.last_name}`,
    scheduled_time: formattedDate?.toISOString() ?? "",
    scheduler_tz: coder?.timezone ?? "",
    question_title: question.question ?? "",
    question_id: question.id ?? 0,
    sender_note: scheduleMessage || "",
  }),
});
if (!response.ok) {
  console.error(
    "Error sending meeting request email:",
    await response.text()
  );
} else {
  const data = await response.json();
  console.log("Meeting request email sent:", data);
}

api/email-templates/MeetingRequest.tsx

import React from "react";
import Link from "next/link";
import { FaCode } from "react-icons/fa6";
import { parseISO, format } from "date-fns";
import { toZonedTime } from "date-fns-tz";

export const MeetingRequest = (props: {
  receiver_name: string;
  scheduler_id: string;
  scheduler_name: string;
  scheduled_time: string;
  scheduler_tz: string;
  question_title: string;
  question_id: number;
  sender_note: string;
}) => {
  const {
    receiver_name,
    scheduler_id,
    scheduler_name,
    scheduled_time,
    scheduler_tz,
    question_title,
    question_id,
    sender_note,
  } = props;

  const date = parseISO(scheduled_time);
  const zonedDate = toZonedTime(date, scheduler_tz);
  const formattedDate = format(zonedDate, "MMMM dd, yyyy 'at' h:mm a");

  return (
    <main className="flex flex-col items-center justify-center bg-slate-200">
      <div className="flex items-center justify-center w-full py-2 bg-slate-300">
        <Link href="https://codingoh.com" className="flex flex-row items-center ml-[10px]">
          <FaCode className="text-3xl" />
          <div className="flex flex-col">
            <span className="text-2xl text-blue-700 font-bold ml-2">CodingOH</span>
            <span className="text-[10.5px] ml-2">Stack Overflow in Real Time</span>
          </div>
        </Link>
      </div>
      <div className="flex flex-col gap-2 w-full p-2 bg-white text-sm">
        <p>Hi {receiver_name},</p>
        <p>
          <Link href={`https://codingoh.com/users/${scheduler_id}`} className="text-blue-500 hover:underline">
            {scheduler_name}
          </Link> 
          has requested a meeting with you on 
          <span className="font-bold underline">{formattedDate}</span>.
          Specically, {scheduler_name.split(" ")[0]} wants to help you out with
          this question:{" "}
          <Link href={`https://codingoh.com/questions/${question_id}`} className="text-blue-500 hover:underline">
            {question_title}
          </Link>.
        </p>
        {sender_note && (
          <div className="bg-slate-300 p-2 rounded-md">
            <p className="font-semibold">Sender Note</p>
            <p>{sender_note}</p>
          </div>
        )}
        <p>
          You can choose to <span className="text-green-500">accept</span>, 
          <span className="text-red-500">reject</span>, or 
          <span className="text-blue-500">reschedule</span> the meeting. If you
          plan on rescheduling the meeting to another time, let 
          {scheduler_name.split(" ")[0]} know what time you would like it
          scheduled at. Make sure to include your time zone to avert confusion
          about the starting time.
        </p>
        <div className="flex items-center justify-center w-full">
          <Link href={`https://codingoh.com/questions/${question_id}`} className="p-2 bg-blue-600 text-white rounded-md">
            Check Out the Meeting Request
          </Link>
        </div>
        <div>
          <p>Best,</p>
          <p className="flex flex-row items-center gap-1 text-blue-700 font-bold">
            CodingOH <FaCode />
          </p>
        </div>
      </div>
    </main>
  );
};

已尝试操作

  • 确认NEXT_PUBLIC_RESEND_KEY已正确配置
  • 确保邮件模板的所有必填字段都已正确传递
  • 检查控制台日志,但错误对象为空

希望能得到解决该问题的指导或帮助!

内容的提问来源于stack exchange,提问作者Judeson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 21:54:57