<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Blaise Tiong</title>
    <description>The latest articles on Forem by Blaise Tiong (@blaise_tiong).</description>
    <link>https://forem.com/blaise_tiong</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2342986%2Ffb57aab2-0a36-46c3-a1de-3192721b8eec.jpg</url>
      <title>Forem: Blaise Tiong</title>
      <link>https://forem.com/blaise_tiong</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/blaise_tiong"/>
    <language>en</language>
    <item>
      <title>How to send emails using Resend and React Email ?</title>
      <dc:creator>Blaise Tiong</dc:creator>
      <pubDate>Mon, 04 Nov 2024 13:28:32 +0000</pubDate>
      <link>https://forem.com/blaise_tiong/how-to-send-emails-using-resend-and-react-email--1f78</link>
      <guid>https://forem.com/blaise_tiong/how-to-send-emails-using-resend-and-react-email--1f78</guid>
      <description>&lt;h2&gt;
  
  
  Why Resend &amp;amp; React Email for your application ?
&lt;/h2&gt;

&lt;p&gt;When developing applications it is common to send emails such as transactional emails, notifications to your users.&lt;br&gt;
However, email templates are notorious of being painful to create due to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inconsistent rendering across email clients&lt;/li&gt;
&lt;li&gt;Responsive email design challenges&lt;/li&gt;
&lt;li&gt;Limited CSS support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where Resend and React Email comes into the picture&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://resend.com/" rel="noopener noreferrer"&gt;Resend&lt;/a&gt; helps us to send the actual emails.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://react.email/" rel="noopener noreferrer"&gt;React Email&lt;/a&gt; helps us to create responsive emails using React components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are a React developer, this will be great email solution for your React/NextJS application.&lt;/p&gt;

&lt;p&gt;Let's go through how I setup my Resend + React Email Setup.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: Install dependencies
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @react-email/components resend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev react-email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Step 2: Create some folders
&lt;/h2&gt;

&lt;p&gt;The following is how I prefer to setup my folder structure for emails. The email sending functions and&lt;br&gt;
email templates are all being stored here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── ...
├── modules/
│   ├── email/
│   │   └── emails/
│   │       ├── ... // This is where we put the email templates
│   │       └── index.ts
│   └── index.ts // Where we put the send email handler
└── ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Get your Resend API key
&lt;/h2&gt;

&lt;p&gt;You will need to sign up for a Resend account and create a Resend API &lt;a href="https://resend.com/api-keys" rel="noopener noreferrer"&gt;key&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2xuhrwhl3whkl321s7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2xuhrwhl3whkl321s7d.png" alt="Showing create api key dialogs in resend interface" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The generated API key will be used to initialise Resend for email sending.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Resend } from "resend";

const resendToken = process.env.RESEND_TOKEN;
// const resendToken = import.meta.env.RESEND_TOKEN; // For Vite based React projects
const resend = new Resend(resendToken);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Create the sendEmail function
&lt;/h2&gt;

&lt;p&gt;What I like to do it to create a wrapper function over Resend's API. So I can have global control over the emails that I wanted to send from my applications.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { type CreateEmailOptions, type CreateEmailRequestOptions, Resend } from "resend";
import { getEnvVariables } from "../utils/getEnvVariables";
import * as emails from "./emails";
import React from "react";

const resendToken = process.env.RESEND_TOKEN;
// const resendToken = import.meta.env.RESEND_TOKEN; // For Vite based React projects
const resend = new Resend(resendToken);

const SENDER = 'onboarding@resend.dev';

// Define the type for the emails object
type Emails = typeof emails;

// Define a union type of the keys of the emails object
type EmailTemplateName = keyof Emails;

// The sendEmail function with type inference for props
export default async function sendEmail&amp;lt;T extends EmailTemplateName&amp;gt;(
  { payload, templateProps, options, template }: {
    payload: Omit&amp;lt;Omit&amp;lt;CreateEmailOptions, 'react'&amp;gt;, 'from'&amp;gt;,
    options?: CreateEmailRequestOptions,
    template: T,
    templateProps: React.ComponentProps&amp;lt;typeof emails[T]&amp;gt;
  }
) {
  const emailComponent = emails[template];
  // @ts-ignore
  const reactElement = React.createElement(emailComponent, templateProps);
  return resend.emails.send(
    {
      ...payload,
      from: SENDER,
      react: reactElement,
    },
    options
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lots of things happening here. In essence what it does is it defines all available emails templates &amp;amp; their corresponding props exported from the &lt;code&gt;emails&lt;/code&gt; folder as a type for the &lt;code&gt;sendEmail&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;This implementation ensures type safety by inferring props based on the selected email template, reducing runtime errors. It simplifies email sending with a reusable function that supports dynamic template selection, enhances developer experience, improves code maintainability, and leverages React and TypeScript for scalable and reliable email handling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Add some templates.
&lt;/h2&gt;

&lt;p&gt;Next is to add some emails into the &lt;code&gt;emails&lt;/code&gt; folder. I refered emails from the &lt;a href="https://demo.react.email/" rel="noopener noreferrer"&gt;React Email Demo Page&lt;/a&gt;.&lt;br&gt;
I will pick the Welcome Email as a sample.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Body, Button, Column, Container, Head, Heading, Html, Img, Link, Preview, Row, Section, Text, Tailwind, } from "@react-email/components";

interface WelcomeEmailProps {
  steps?: {
    id: number;
    Description: React.ReactNode;
  }[];
  links?: string[];
}

const baseUrl = process.env.VERCEL_URL
  ? `https://${process.env.VERCEL_URL}`
  : "";

const PropDefaults: WelcomeEmailProps = {
  steps: [
    {
      id: 1,
      Description: (
        &amp;lt;li className="mb-20" key={1} &amp;gt;
          &amp;lt;strong&amp;gt;Deploy your first project.&amp;lt;/strong&amp;gt;{" "}
          &amp;lt; Link &amp;gt; Connect to Git, choose a template&amp;lt;/ Link &amp;gt;, or manually deploy a
          project you've been working on locally.
        &amp;lt;/li&amp;gt;
      ),
    },
  ],
  links: ["Visit the forums", "Read the docs", "Contact an expert"],
};

export const Welcome = ({
  steps = PropDefaults.steps,
  links = PropDefaults.links,
}: WelcomeEmailProps) =&amp;gt; {
  return (
    &amp;lt;Html&amp;gt;
      &amp;lt;Head /&amp;gt;
      &amp;lt;Preview&amp;gt; Netlify Welcome &amp;lt;/Preview&amp;gt;
      &amp;lt;Tailwind
        config={{
          theme: {
            extend: {
              colors: { brand: "#2250f4", offwhite: "#fafbfb" },
              spacing: { 0: "0px", 20: "20px", 45: "45px" },
            },
          },
        }
        }
      &amp;gt;
        &amp;lt;Body className="bg-offwhite text-base font-sans" &amp;gt;
          &amp;lt;Img
            src={`${baseUrl}/static/netlify-logo.png`}
            width="184"
            height="75"
            alt="Netlify"
            className="mx-auto my-20"
          /&amp;gt;
          &amp;lt;Container className="bg-white p-45" &amp;gt;
            &amp;lt;Heading className="text-center my-0 leading-8" &amp;gt;
              Welcome to Netlify
            &amp;lt;/Heading&amp;gt;

            &amp;lt; Section &amp;gt;
              &amp;lt;Row&amp;gt;
                &amp;lt;Text className="text-base" &amp;gt;
                  Congratulations! You're joining over 3 million developers
                  around the world who use Netlify to build and ship sites,
                  stores, and apps.
                &amp;lt;/Text&amp;gt;

                &amp;lt; Text className="text-base" &amp;gt; Here's how to get started:&amp;lt;/Text&amp;gt;
              &amp;lt;/Row&amp;gt;
            &amp;lt;/Section&amp;gt;

            &amp;lt;ul&amp;gt; {steps?.map(({ Description }) =&amp;gt; Description)}&amp;lt;/ul&amp;gt;

            &amp;lt;Section className="text-center" &amp;gt;
              &amp;lt;Button className="bg-brand text-white rounded-lg py-3 px-[18px]" &amp;gt;
                Go to your dashboard
              &amp;lt;/Button&amp;gt;
            &amp;lt;/Section&amp;gt;

            &amp;lt;Section className="mt-45" &amp;gt;
              &amp;lt;Row&amp;gt;
                {links?.map((link) =&amp;gt; (
                  &amp;lt;Column key={link} &amp;gt;
                    &amp;lt;Link className="text-black underline font-bold" &amp;gt;
                      {link}
                    &amp;lt;/Link&amp;gt;{" "}
                    &amp;lt; span className="text-green-500" &amp;gt;→&amp;lt;/span&amp;gt;
                  &amp;lt;/Column&amp;gt;
                ))}
              &amp;lt;/Row&amp;gt;
            &amp;lt;/Section&amp;gt;
          &amp;lt;/Container&amp;gt;

          &amp;lt; Container className="mt-20" &amp;gt;
            &amp;lt;Section&amp;gt;
              &amp;lt;Row&amp;gt;
                &amp;lt;Column className="text-right px-20" &amp;gt;
                  &amp;lt;Link&amp;gt;Unsubscribe &amp;lt;/Link&amp;gt;
                &amp;lt;/Column&amp;gt;
                &amp;lt; Column className="text-left" &amp;gt;
                  &amp;lt;Link&amp;gt;Manage Preferences &amp;lt;/Link&amp;gt;
                &amp;lt;/Column&amp;gt;
              &amp;lt;/Row&amp;gt;
            &amp;lt;/Section&amp;gt;
            &amp;lt; Text className="text-center text-gray-400 mb-45" &amp;gt;
              Netlify, 44 Montgomery Street, Suite 300 San Francisco, CA
            &amp;lt;/Text&amp;gt;
          &amp;lt;/Container&amp;gt;
        &amp;lt;/Body&amp;gt;
      &amp;lt;/Tailwind&amp;gt;
    &amp;lt;/Html&amp;gt;
  );
};

export default Welcome;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Export the email template from the &lt;code&gt;emails&lt;/code&gt; folder. This template will be registered as a props of the &lt;code&gt;sendEmail&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
export * from "./welcome-email"
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 6: Try it out.
&lt;/h2&gt;

&lt;p&gt;Invoke your &lt;code&gt;sendEmail&lt;/code&gt; function in your project as so, you can set &lt;code&gt;to:&lt;/code&gt; as your personal email to try it out.&lt;/p&gt;

&lt;p&gt;You should see available email templates in the autocomplete.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwih6wl74u0gz797nnivi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwih6wl74u0gz797nnivi.png" alt="Showing autocomplete working in code editor" width="800" height="413"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {sendEmail} from "@/modules/email";

await sendEmail({
  payload: {
    subject: "Please reply to me now",
    to: "example@gmail.com",
  },
  template: "Welcome",
  templateProps: {
    steps: [
      {
        id: 1,
        Description: "Some description",
      },
    ],
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to preview your email templates ?
&lt;/h2&gt;

&lt;p&gt;Resend provides a neat way to preview your email templates. Head over to your &lt;code&gt;package.json&lt;/code&gt;.&lt;br&gt;
Add a new script &lt;code&gt;email dev --dir &amp;lt;-directory of your email templates-&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "email-dev": "email dev --dir src/modules/email/emails"
},
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;npm run email-dev&lt;/code&gt; should give you a neat previewer like below. The email in the previewer should reflect the changes that you made within the email folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb37fx0g02eg4997rzuv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb37fx0g02eg4997rzuv.png" alt="Resend Preview" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Automatic setup
&lt;/h2&gt;

&lt;p&gt;To automate the setup above of React Email &amp;amp; Resend to your project. You can utilize &lt;a href="https://c0nfig.dev/" rel="noopener noreferrer"&gt;c0nfig&lt;/a&gt; to automate the process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initialise c0nfig
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx k0nfig@latest init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Run email command
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx k0nfig@latest run https://c0nfig.dev/cli/email-setup.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add in more email templates
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx k0nfig@latest run https://c0nfig.dev/cli/email-add.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojns7ti4t72uccqk0s7a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojns7ti4t72uccqk0s7a.png" alt="Showing autocomplete working in code editor" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By integrating &lt;strong&gt;Resend&lt;/strong&gt; and &lt;strong&gt;React Email&lt;/strong&gt; into your React application, you can overcome the common challenges of creating email templates—such as inconsistent rendering across clients and limited CSS support. This approach allows you to leverage your React skills to build responsive and maintainable email templates efficiently. The &lt;code&gt;sendEmail&lt;/code&gt; function provides a type-safe, reusable way to send emails with dynamic templates, enhancing developer experience and code reliability. Additionally, using tools like &lt;strong&gt;c0nfig&lt;/strong&gt; can automate and streamline the setup process, saving you time and effort. With these tools, you can focus on delivering great features to your users without getting bogged down by the complexities of email template development.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>react</category>
      <category>nextjs</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to send emails using Resend and React Email ?</title>
      <dc:creator>Blaise Tiong</dc:creator>
      <pubDate>Mon, 04 Nov 2024 13:28:32 +0000</pubDate>
      <link>https://forem.com/blaise_tiong/how-to-send-emails-using-resend-and-react-email--hd0</link>
      <guid>https://forem.com/blaise_tiong/how-to-send-emails-using-resend-and-react-email--hd0</guid>
      <description>&lt;h2&gt;
  
  
  Why Resend &amp;amp; React Email for your application ?
&lt;/h2&gt;

&lt;p&gt;When developing applications it is common to send emails such as transactional emails, notifications to your users.&lt;br&gt;
However, email templates are notorious of being painful to create due to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inconsistent rendering across email clients&lt;/li&gt;
&lt;li&gt;Responsive email design challenges&lt;/li&gt;
&lt;li&gt;Limited CSS support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where Resend and React Email comes into the picture&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://resend.com/" rel="noopener noreferrer"&gt;Resend&lt;/a&gt; helps us to send the actual emails.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://react.email/" rel="noopener noreferrer"&gt;React Email&lt;/a&gt; helps us to create responsive emails using React components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are a React developer, this will be great email solution for your React/NextJS application.&lt;/p&gt;

&lt;p&gt;Let's go through how I setup my Resend + React Email Setup.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: Install dependencies
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @react-email/components resend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev react-email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Step 2: Create some folders
&lt;/h2&gt;

&lt;p&gt;The following is how I prefer to setup my folder structure for emails. The email sending functions and&lt;br&gt;
email templates are all being stored here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── ...
├── modules/
│   ├── email/
│   │   └── emails/
│   │       ├── ... // This is where we put the email templates
│   │       └── index.ts
│   └── index.ts // Where we put the send email handler
└── ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Get your Resend API key
&lt;/h2&gt;

&lt;p&gt;You will need to sign up for a Resend account and create a Resend API &lt;a href="https://resend.com/api-keys" rel="noopener noreferrer"&gt;key&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2xuhrwhl3whkl321s7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2xuhrwhl3whkl321s7d.png" alt="Showing create api key dialogs in resend interface" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The generated API key will be used to initialise Resend for email sending.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Resend } from "resend";

const resendToken = process.env.RESEND_TOKEN;
// const resendToken = import.meta.env.RESEND_TOKEN; // For Vite based React projects
const resend = new Resend(resendToken);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Create the sendEmail function
&lt;/h2&gt;

&lt;p&gt;What I like to do it to create a wrapper function over Resend's API. So I can have global control over the emails that I wanted to send from my applications.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { type CreateEmailOptions, type CreateEmailRequestOptions, Resend } from "resend";
import { getEnvVariables } from "../utils/getEnvVariables";
import * as emails from "./emails";
import React from "react";

const resendToken = process.env.RESEND_TOKEN;
// const resendToken = import.meta.env.RESEND_TOKEN; // For Vite based React projects
const resend = new Resend(resendToken);

const SENDER = 'onboarding@resend.dev';

// Define the type for the emails object
type Emails = typeof emails;

// Define a union type of the keys of the emails object
type EmailTemplateName = keyof Emails;

// The sendEmail function with type inference for props
export default async function sendEmail&amp;lt;T extends EmailTemplateName&amp;gt;(
  { payload, templateProps, options, template }: {
    payload: Omit&amp;lt;Omit&amp;lt;CreateEmailOptions, 'react'&amp;gt;, 'from'&amp;gt;,
    options?: CreateEmailRequestOptions,
    template: T,
    templateProps: React.ComponentProps&amp;lt;typeof emails[T]&amp;gt;
  }
) {
  const emailComponent = emails[template];
  // @ts-ignore
  const reactElement = React.createElement(emailComponent, templateProps);
  return resend.emails.send(
    {
      ...payload,
      from: SENDER,
      react: reactElement,
    },
    options
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lots of things happening here. In essence what it does is it defines all available emails templates &amp;amp; their corresponding props exported from the &lt;code&gt;emails&lt;/code&gt; folder as a type for the &lt;code&gt;sendEmail&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;This implementation ensures type safety by inferring props based on the selected email template, reducing runtime errors. It simplifies email sending with a reusable function that supports dynamic template selection, enhances developer experience, improves code maintainability, and leverages React and TypeScript for scalable and reliable email handling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Add some templates.
&lt;/h2&gt;

&lt;p&gt;Next is to add some emails into the &lt;code&gt;emails&lt;/code&gt; folder. I refered emails from the &lt;a href="https://demo.react.email/" rel="noopener noreferrer"&gt;React Email Demo Page&lt;/a&gt;.&lt;br&gt;
I will pick the Welcome Email as a sample.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Body, Button, Column, Container, Head, Heading, Html, Img, Link, Preview, Row, Section, Text, Tailwind, } from "@react-email/components";

interface WelcomeEmailProps {
  steps?: {
    id: number;
    Description: React.ReactNode;
  }[];
  links?: string[];
}

const baseUrl = process.env.VERCEL_URL
  ? `https://${process.env.VERCEL_URL}`
  : "";

const PropDefaults: WelcomeEmailProps = {
  steps: [
    {
      id: 1,
      Description: (
        &amp;lt;li className="mb-20" key={1} &amp;gt;
          &amp;lt;strong&amp;gt;Deploy your first project.&amp;lt;/strong&amp;gt;{" "}
          &amp;lt; Link &amp;gt; Connect to Git, choose a template&amp;lt;/ Link &amp;gt;, or manually deploy a
          project you've been working on locally.
        &amp;lt;/li&amp;gt;
      ),
    },
  ],
  links: ["Visit the forums", "Read the docs", "Contact an expert"],
};

export const Welcome = ({
  steps = PropDefaults.steps,
  links = PropDefaults.links,
}: WelcomeEmailProps) =&amp;gt; {
  return (
    &amp;lt;Html&amp;gt;
      &amp;lt;Head /&amp;gt;
      &amp;lt;Preview&amp;gt; Netlify Welcome &amp;lt;/Preview&amp;gt;
      &amp;lt;Tailwind
        config={{
          theme: {
            extend: {
              colors: { brand: "#2250f4", offwhite: "#fafbfb" },
              spacing: { 0: "0px", 20: "20px", 45: "45px" },
            },
          },
        }
        }
      &amp;gt;
        &amp;lt;Body className="bg-offwhite text-base font-sans" &amp;gt;
          &amp;lt;Img
            src={`${baseUrl}/static/netlify-logo.png`}
            width="184"
            height="75"
            alt="Netlify"
            className="mx-auto my-20"
          /&amp;gt;
          &amp;lt;Container className="bg-white p-45" &amp;gt;
            &amp;lt;Heading className="text-center my-0 leading-8" &amp;gt;
              Welcome to Netlify
            &amp;lt;/Heading&amp;gt;

            &amp;lt; Section &amp;gt;
              &amp;lt;Row&amp;gt;
                &amp;lt;Text className="text-base" &amp;gt;
                  Congratulations! You're joining over 3 million developers
                  around the world who use Netlify to build and ship sites,
                  stores, and apps.
                &amp;lt;/Text&amp;gt;

                &amp;lt; Text className="text-base" &amp;gt; Here's how to get started:&amp;lt;/Text&amp;gt;
              &amp;lt;/Row&amp;gt;
            &amp;lt;/Section&amp;gt;

            &amp;lt;ul&amp;gt; {steps?.map(({ Description }) =&amp;gt; Description)}&amp;lt;/ul&amp;gt;

            &amp;lt;Section className="text-center" &amp;gt;
              &amp;lt;Button className="bg-brand text-white rounded-lg py-3 px-[18px]" &amp;gt;
                Go to your dashboard
              &amp;lt;/Button&amp;gt;
            &amp;lt;/Section&amp;gt;

            &amp;lt;Section className="mt-45" &amp;gt;
              &amp;lt;Row&amp;gt;
                {links?.map((link) =&amp;gt; (
                  &amp;lt;Column key={link} &amp;gt;
                    &amp;lt;Link className="text-black underline font-bold" &amp;gt;
                      {link}
                    &amp;lt;/Link&amp;gt;{" "}
                    &amp;lt; span className="text-green-500" &amp;gt;→&amp;lt;/span&amp;gt;
                  &amp;lt;/Column&amp;gt;
                ))}
              &amp;lt;/Row&amp;gt;
            &amp;lt;/Section&amp;gt;
          &amp;lt;/Container&amp;gt;

          &amp;lt; Container className="mt-20" &amp;gt;
            &amp;lt;Section&amp;gt;
              &amp;lt;Row&amp;gt;
                &amp;lt;Column className="text-right px-20" &amp;gt;
                  &amp;lt;Link&amp;gt;Unsubscribe &amp;lt;/Link&amp;gt;
                &amp;lt;/Column&amp;gt;
                &amp;lt; Column className="text-left" &amp;gt;
                  &amp;lt;Link&amp;gt;Manage Preferences &amp;lt;/Link&amp;gt;
                &amp;lt;/Column&amp;gt;
              &amp;lt;/Row&amp;gt;
            &amp;lt;/Section&amp;gt;
            &amp;lt; Text className="text-center text-gray-400 mb-45" &amp;gt;
              Netlify, 44 Montgomery Street, Suite 300 San Francisco, CA
            &amp;lt;/Text&amp;gt;
          &amp;lt;/Container&amp;gt;
        &amp;lt;/Body&amp;gt;
      &amp;lt;/Tailwind&amp;gt;
    &amp;lt;/Html&amp;gt;
  );
};

export default Welcome;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Export the email template from the &lt;code&gt;emails&lt;/code&gt; folder. This template will be registered as a props of the &lt;code&gt;sendEmail&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
export * from "./welcome-email"
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 6: Try it out.
&lt;/h2&gt;

&lt;p&gt;Invoke your &lt;code&gt;sendEmail&lt;/code&gt; function in your project as so, you can set &lt;code&gt;to:&lt;/code&gt; as your personal email to try it out.&lt;/p&gt;

&lt;p&gt;You should see available email templates in the autocomplete.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwih6wl74u0gz797nnivi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwih6wl74u0gz797nnivi.png" alt="Showing autocomplete working in code editor" width="800" height="413"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {sendEmail} from "@/modules/email";

await sendEmail({
  payload: {
    subject: "Please reply to me now",
    to: "example@gmail.com",
  },
  template: "Welcome",
  templateProps: {
    steps: [
      {
        id: 1,
        Description: "Some description",
      },
    ],
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to preview your email templates ?
&lt;/h2&gt;

&lt;p&gt;Resend provides a neat way to preview your email templates. Head over to your &lt;code&gt;package.json&lt;/code&gt;.&lt;br&gt;
Add a new script &lt;code&gt;email dev --dir &amp;lt;-directory of your email templates-&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "email-dev": "email dev --dir src/modules/email/emails"
},
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;npm run email-dev&lt;/code&gt; should give you a neat previewer like below. The email in the previewer should reflect the changes that you made within the email folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb37fx0g02eg4997rzuv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb37fx0g02eg4997rzuv.png" alt="Resend Preview" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Automatic setup
&lt;/h2&gt;

&lt;p&gt;To automate the setup above of React Email &amp;amp; Resend to your project. You can utilize &lt;a href="https://c0nfig.dev/" rel="noopener noreferrer"&gt;c0nfig&lt;/a&gt; to automate the process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initialise c0nfig
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx k0nfig@latest init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Run email command
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx k0nfig@latest run https://c0nfig.dev/cli/email-setup.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add in more email templates
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx k0nfig@latest run https://c0nfig.dev/cli/email-add.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojns7ti4t72uccqk0s7a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojns7ti4t72uccqk0s7a.png" alt="Showing autocomplete working in code editor" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By integrating &lt;strong&gt;Resend&lt;/strong&gt; and &lt;strong&gt;React Email&lt;/strong&gt; into your React application, you can overcome the common challenges of creating email templates—such as inconsistent rendering across clients and limited CSS support. This approach allows you to leverage your React skills to build responsive and maintainable email templates efficiently. The &lt;code&gt;sendEmail&lt;/code&gt; function provides a type-safe, reusable way to send emails with dynamic templates, enhancing developer experience and code reliability. Additionally, using tools like &lt;strong&gt;c0nfig&lt;/strong&gt; can automate and streamline the setup process, saving you time and effort. With these tools, you can focus on delivering great features to your users without getting bogged down by the complexities of email template development.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
