React Email
repository·canary·Indexed 12 days ago
https://github.com/resend/react-emailA collection of high-quality, unstyled React components for creating responsive, modern emails. It includes a CLI for development and exporting to HTML, a rich text editor via @react-email/editor, and a rendering engine via @react-email/render to transform components into static HTML strings.
What's inside React Email
- The React Email Editor is an embeddable visual editor designed specifically for composing React Email templates. It is built on top of TipTap and ProseMirror, meaning standard TipTap and ProseMirror concepts apply. The editor produces email-ready HTML and plain text, and supports features like rich text editing, bubble menus, slash commands, multi-column layouts, and email theming.
Integrate React Email with email service providers
canaryReact Email supports various email service providers (ESPs) to send your rendered emails. You can integrate with providers such as Resend, Nodemailer, Mailgun, SendGrid, Postmark, AWS SES, Azure Communication Email, MailerSend, Scaleway, and Plunk. Each integration typically involves using a specific package or adapter to bridge your React-rendered email components with the provider's API.Email client compatibility for React Email components
canaryAll React Email components are tested for compatibility across the most popular email clients to ensure consistent rendering. Supported clients include:
- Gmail
- Apple Mail
- Outlook
- Yahoo! Mail
- HEY
- Superhuman
What is EmailNode?
canaryEmailNodeis a base class that extends TipTap'sNodeclass. It adds arenderToReactEmail()method, which allows you to define how a specific node should be serialized into HTML when exporting an email viacomposeReactEmail. This ensures that TipTap editor content is correctly transformed into email-safe React components.How the Button component works
canaryIn email development, what are often referred to as 'buttons' are actually links. TheButtoncomponent implements this by rendering an<a>tag that is visually styled to resemble a<button>element, ensuring better compatibility and functionality within email clients.Parsing HTML content with links
canaryThe editor automatically parses
<a>tags from HTML strings into its internal link model.Note that the link extension is included in
StarterKitwithopenOnClickdisabled by default. This behavior ensures that clicking a link in edit mode focuses the link rather than navigating the user away from the editor.const content = ` <p>Visit <a href="https://react.email" target="_blank">React Email</a> for more info.</p> `;Use structural components for email layout
canaryReact Email provides several components to build the skeleton of your email.
<Html>: The root wrapper. Always use this as the outermost component. Supportslanganddirprops.<Head>: Contains document metadata like<title>or<style>. Place this inside<Tailwind>.<Body>: Wraps the main content of the email.<Container>: Centers content horizontally with a default max-width of37.5em.<Section>: A layout block that can contain rows and columns. It renders a<table role="presentation">by default.<Row>and<Column>: Used together to create grid-like layouts.<Row>separates content horizontally, and<Column>separates content vertically. Note:<Column>must always be a child of<Row>.
Layout Tip: If you use a raw
<table>for layout instead of these components, you must manually addrole="presentation"for accessibility.import { Section, Row, Column } from 'react-email'; <Section> <Row> <Column className="w-1/2 p-2 align-top"> Left column content </Column> <Column className="w-1/2 p-2 align-top"> Right column content </Column> </Row> </Section>Convert React Email components to HTML strings
canaryTo use React Email components with an email service provider, you must convert the React components into a raw HTML string. This is achieved using therenderutility. This step is necessary because email service providers require HTML content rather than React elements.Implement Dark Mode without dark: selectors
canaryThe Tailwinddark:variant is not supported in most email clients. To support dark mode, do not usedark:bg-gray-900or similar classes. Instead, apply the desired dark theme colors directly to the styles if a dark theme is requested.Handle email client CSS limitations
canaryEmail clients have significant CSS restrictions. Avoid the following:
- Flexbox/Grid: Use
<Row>and<Column>components or HTML tables instead. - Media Queries: Responsive prefixes like
sm:,md:,lg:, andxl:are not supported. - Theme Selectors:
dark:andlight:prefixes do not work. - SVG/WEBP: Use only PNG or JPEG formats for images.
- rem units: Always use
pixelBasedPresetto ensure pixel conversion.
Border Handling
When applying borders, you must explicitly specify the border style. Simply using
borderwithout a style (likeborder-solid) may fail in some clients.// Correct: specify border style <div className="border-solid border border-gray-300" /> // Correct: single side border with reset <div className="border-none border-l border-solid border-l-gray-300" /> // Incorrect: missing border style <div className="border border-gray-300" />- Flexbox/Grid: Use
Core React Email Components
canaryReact Email provides a set of standard components designed to handle the complexities of email markup (like table-based layouts) so you can build emails using modern React patterns. Key components include:
Html: A React component used to wrap your entire email content.Container: The main wrapper component that holds your email content.Button: A component for building clickable buttons within emails.Text: A component for rendering blocks of text with appropriate spacing.
How the Editor Event Bus works
canaryThe editor uses a singleton
editorEventBusto facilitate communication between components. It is built on the browser's nativeCustomEventAPI, and all events are prefixed with@react-email/editor:. This allows different parts of the editor to react to actions (like menu interactions) or trigger custom logic without direct component coupling.import { editorEventBus } from '@react-email/editor/core';