webdevcody / wdc-saas-starter-kit

https://wdcstarterkit.com/
MIT License
867 stars 221 forks source link

Add resend mock for development #22

Open jerebenitez opened 1 month ago

jerebenitez commented 1 month ago

I don't know if this is a good practice or not, which is why I created an issue instead of just doing a pull request, but I like having a mock service for emails while on development that just prints the resulting e-mail to console.

Right now, my src/lib/send-email.tsx looks like this:

import { Resend } from "resend";
import { render } from '@react-email/render';

import { env } from "@/env";
import { ReactNode } from "react";

const resend = new Resend(env.EMAIL_SERVER_PASSWORD);

export async function sendEmail(
  email: string,
  subject: string,
  body: ReactNode
) {

  if (env.NODE_ENV === "production") {
    const { error } = await resend.emails.send({
      from: env.EMAIL_FROM,
      to: email,
      subject,
      react: <>{body}</>,
    });

    if (error) {
      throw error;
    }
  } else {
    const Body = () => <>{body}</>

    console.log(`from: ${env.EMAIL_FROM}`)
    console.log(`to: ${email}`)
    console.log(`subject: ${subject}`)
    console.log(`body: ${render(<Body />, { plainText: true })}`)
  }

}

using render from react-email. I can make the pull request if needed, but I wanted to know your opinion on this before.

Cheers!