Skip to content

Instantly share code, notes, and snippets.

@amosbastian
Created June 13, 2023 15:27
Show Gist options
  • Save amosbastian/8a9d5d53ba94858a35b4a0a7155f0dc5 to your computer and use it in GitHub Desktop.
Save amosbastian/8a9d5d53ba94858a35b4a0a7155f0dc5 to your computer and use it in GitHub Desktop.
Mailing.run + Resend.com
import { openPreview, render } from "mailing-core";
import { Resend } from "resend";
import type { CreateEmailOptions, CreateEmailRequestOptions } from "resend/build/src/emails/interfaces";
const BRAND_NAME = "Template";
const BASE_URL = "https://example.com";
const resend = new Resend(process.env["RESEND_API_KEY"]);
function extractHostname(url: string): string {
const parsedUrl = new URL(url);
return parsedUrl.hostname;
}
type Payload = Omit<CreateEmailOptions, "from"> & {
component: React.ReactElement<any, string | React.JSXElementConstructor<any>>;
from?: string;
};
export function sendEmail(
{ component, from = `${BRAND_NAME} <system@${extractHostname(BASE_URL)}>`, ...rest }: Payload,
options?: CreateEmailRequestOptions | undefined,
) {
const { html } = render(component);
if (process.env.NODE_ENV === "development") {
return openPreview({ component, html, from, ...rest }, { from, html, ...rest }, "http://localhost:3883");
}
return resend.emails.send({ html, from, ...rest }, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment