webdevcody / wdc-saas-starter-kit

https://wdcstarterkit.com/
MIT License
1.17k stars 274 forks source link

Add resend mock for development #22

Open jerebenitez opened 3 months ago

jerebenitez commented 3 months 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!

webdevcody commented 1 month ago

that's a good idea, I'm open to a pull request as long as it's a documented env variable.