How to send two emails to two different users? Resend.com

367 views Asked by At

I am using resend.com to try and send an email with different variables to two different users.

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);
  }
};

This sends to player1 but not to player2 even though the emails are correct for both. How can I send both emails to both users?

Their api allows you to send up to 50 emails by passing an array of strings into 'to' but this is not ideal as it means both parties can see each others email https://resend.com/docs/api-reference/emails/send-email

1

There are 1 answers

1
Akshay On

i guess its already late to answer you as its been 5months already about this question.

But for the people who are still learning and searching for this query!

here:

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'));
}