next.js 如何向两个不同的用户发送两封电子邮件?Resend.com

g6ll5ycj  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(97)

我正在使用resend.com尝试向两个不同的用户发送带有不同变量的电子邮件。

import type { NextApiRequest, NextApiResponse } from "next";
import { EmailTemplate } from "../../components/emails/EmailTemplate";
import { Resend } from "resend";
import { supabase } from "../../../initSupabase";

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

export default async (req: NextApiRequest, res: NextApiResponse) => {
  try {
    const { player1, player2, player1Email, player2Email } = req.body;

    const player1Data = await resend.emails.send({
      from: "Acme <[email protected]>",
      to: [player1Email],
      subject: "Hello player 1",
      react: EmailTemplate({ firstName: player1, secondName: player1Email }),
    });

    const player2Data = await resend.emails.send({
      from: "Acme <[email protected]>",
      to: [player2Email],
      subject: "Hello player 2",
      react: EmailTemplate({ firstName: player2, secondName: player2Email }),
    });

    res.status(200).json({ player1Data, player2Data });
  } catch (error) {
    res.status(400).json(error);
  }
};

字符串
这会发送给玩家1,但不会发送给玩家2,即使电子邮件对两者都是正确的。我如何向两个用户发送两封电子邮件?
他们的API允许你通过向“to”传递一组字符串来发送多达50封电子邮件,但这并不理想,因为这意味着双方都可以看到对方的电子邮件https://resend.com/docs/api-reference/emails/send-email

yeotifhr

yeotifhr1#

我想现在回答你已经晚了,因为这个问题已经过去5个月了。
但是对于那些仍然在学习和搜索这个查询的人来说!
这里:

import { NextResponse } from 'next/server';
import { Resend } from 'resend';
import { EmailTemplate } from "../../components/emails/EmailTemplate";

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

export async function POST(req) {
  if (req.method === 'POST') {
    const { player1, player2, player1Email, player2Email } = req.json();

    try {
      const { player1Data } = await resend.emails.send({
        from: "Acme <[email protected]>",
        to: [player1Email],
        subject: "Hello player 1",
        text: '',
        react: EmailTemplate({ firstName: player1, secondName: player1Email })
      });

      const { player2Data } = await resend.emails.send({
        from: "Acme <[email protected]>",
        to: [player2Email],
        subject: "Hello player 2",
        text: '',
        react: EmailTemplate({ firstName: player2, secondName: player2Email })
      });
      return NextResponse.json({player1Data, player2Data});
    } catch (error) {
      return NextResponse.json({ error });
    }
  }

  return NextResponse.error(new Error('Invalid method'));
}

字符串

相关问题