A shared layout and four components that turn Tailwind classes into inline styles at render time — and why the pixel preset, not the class names, decides whether Outlook keeps your spacing.
The Problem
Your welcome email is a React component. It looks right in the browser, so you render it to a string and hand the string to your mail provider.
export function WelcomeMail({ name }: { name: string }) { return ( <div className="mx-auto max-w-[600px] p-6 text-sm text-gray-700"> <h1 className="mb-4 text-2xl font-bold">Welcome, {name}</h1> <p className="mb-6">Your account is ready. Sign in to get started.</p> <a className="rounded-lg bg-teal-700 px-4 py-2 text-white" href="https://example.com/dashboard" > Open dashboard </a> </div> ); } Gmail web renders it. Outlook desktop renders a full-width white page with 12px text, no padding, and a link that looks like a link. Apple Mail renders the button but at a font size you never set. Nothing threw. The HTML is valid. The email is broken.
You try the three obvious fixes.
- Move every style into a
styleobject. It works in Outlook, but now you maintain two styling systems, andhover:, media queries, anddark:have no object form.
const el = <div style={{ maxWidth: 600, padding: 24, fontSize: 14 }} />; - Put the classes in a
<style>block in<Head>. Gmail web honors it. Outlook.com and the Gmail iOS app for third-party accounts drop it, so the mail arrives unstyled.
<Head> <style>{`.card { padding: 24px; max-width: 600px; }`}</style> </Head> - Switch the whole thing to MJML. You get reliable table markup, but you also get a second templating language. Props, types, and every shared component stop at the boundary, and your app logic has to be re-expressed as MJML includes.
You want to author email in the same React and Tailwind vocabulary as the app, but clients only honor a narrow, inlined subset of CSS that the browser never had to explain to you.
The resolution is a layout that renders the same tree for every mail, with the design tokens in one place and a compile step that inlines them:
export function EmailLayout({ appName, previewText, supportMail, children, }: { appName: string; previewText: string; supportMail: string; children: React.ReactNode; }) { return ( <Html lang="en"> <Tailwind config={{ presets: [pixelBasedPreset], theme: { extend: { colors: TOKENS } }, }} > <Head /> <Body className="bg-background text-foreground"> <Preview>{previewText}</Preview> <Container className="mx-auto max-w-[600px] border border-border p-5"> {children} </Container> </Body> </Tailwind> </Html> ); } The Mental Model
Think of a printing press with one plate.
- Identity: the JSX tree is the plate. It is the only original. The HTML you send is a print, and you can make as many prints as you like without redrawing.
- Update timing: Tailwind classes are compiled when you run the press (
render()), not when the app builds. Change a class in the plate and the next print changes; the client never sees the class name. - Read timing: clients read only what is physically on the page — inline styles. Anything left in a stylesheet is ink that never touched the paper.
Two consequences follow. First, a class that cannot be inlined (hover:, group-hover:, complex selectors) cannot be trusted, no matter how good it looks in the preview. Second, the pixel preset is not cosmetic: without it, p-6 prints as 1.5rem, and the Word rendering engine behind Outlook desktop does not know what a rem is.
The template is the plate; the inbox only ever sees the print.
How to Use It
Install React Email. The preview app ships separately.
pnpm add react-email pnpm add -D @react-email/ui src/ └── mail/ ├── components/ │ ├── email-layout.tsx │ ├── email-button.tsx │ ├── email-heading.tsx │ ├── email-info-card.tsx │ ├── email-link.tsx │ └── index.ts ├── templates/ │ ├── welcome-mail.tsx │ ├── reset-password-mail.tsx │ └── index.ts ├── mail.types.ts └── mail-service.tsx src/mail/components/email-layout.tsx — one layout owns the palette, the width, and the footer:
import { Body, Container, Head, Html, Link, Preview, Section, Tailwind, Text, pixelBasedPreset, } from "react-email"; const TOKENS = { background: "#ffffff", foreground: "#111827", muted: "#6b7280", border: "#e5e7eb", card: "#f9fafb", primary: "#0f766e", "primary-foreground": "#f0fdfa", }; const YEAR = new Date().getFullYear(); export interface EmailLayoutProps { appName: string; previewText: string; supportMail: string; children: React.ReactNode; } export function EmailLayout({ appName, previewText, supportMail, children, }: EmailLayoutProps) { return ( <Html lang="en"> <Tailwind config={{ presets: [pixelBasedPreset], theme: { extend: { colors: TOKENS } }, }} > <Head /> <Body className="m-0 bg-background px-2 font-sans text-foreground"> <Preview>{previewText}</Preview> <Container className="mx-auto my-10 max-w-[600px] rounded border border-solid border-border p-5"> <Section className="mb-6 text-center"> <Text className="m-0 text-2xl font-bold">{appName}</Text> </Section> <Section className="text-sm">{children}</Section> <Section className="mt-8 border-t border-border pt-4 text-center"> <Text className="m-0 text-sm text-muted"> Need help? Contact us at{" "} <Link href={`mailto:${supportMail}`} className="text-primary"> {supportMail} </Link> </Text> <Text className="mt-2 text-xs text-muted"> {`© ${YEAR} ${appName}. All rights reserved.`} </Text> </Section> </Container> </Body> </Tailwind> </Html> ); } src/mail/components/email-button.tsx — the anchor is the button. There is no JavaScript in email:
import { Button } from "react-email"; export function EmailButton({ children, ...props }: React.ComponentProps<typeof Button>) { return ( <Button className="inline-block rounded-lg bg-primary px-5 py-3 text-sm font-semibold text-primary-foreground no-underline" {...props} > {children} </Button> ); } src/mail/components/email-heading.tsx:
import { Text } from "react-email"; export function EmailHeading({ children }: { children: React.ReactNode }) { return ( <Text className="mt-0 mb-2 text-lg font-bold text-foreground"> {children} </Text> ); } src/mail/components/email-info-card.tsx — a bordered box for security context:
import { Section } from "react-email"; export function EmailInfoCard({ children }: { children: React.ReactNode }) { return ( <Section className="my-4 rounded-md border border-border bg-card p-4"> {children} </Section> ); } src/mail/components/email-link.tsx — long URLs must wrap, or they blow out the 600px column:
import { Link } from "react-email"; export function EmailLink({ href, children, ...props }: React.ComponentProps<typeof Link>) { return ( <Link href={href} className="text-sm break-all text-primary" {...props}> {children} </Link> ); } src/mail/components/index.ts:
export { EmailLayout, type EmailLayoutProps } from "./email-layout"; export { EmailButton } from "./email-button"; export { EmailHeading } from "./email-heading"; export { EmailInfoCard } from "./email-info-card"; export { EmailLink } from "./email-link"; src/mail/templates/welcome-mail.tsx:
import { Section, Text } from "react-email"; import { EmailButton, EmailHeading, EmailLayout, EmailLink, } from "@/mail/components"; export interface WelcomeMailProps { userName: string; appName: string; supportMail: string; dashboardUrl: string; } export default function WelcomeMail({ userName, appName, supportMail, dashboardUrl, }: WelcomeMailProps) { return ( <EmailLayout appName={appName} previewText="Your account is ready." supportMail={supportMail} > <EmailHeading>Welcome to {appName}</EmailHeading> <Text>Hello {userName},</Text> <Text>Your account is ready. Here is the fastest way in:</Text> <Section className="my-6 text-center"> <EmailButton href={dashboardUrl}>Open dashboard</EmailButton> </Section> <Text className="text-sm text-muted"> If the button does not work, paste this link into your browser: </Text> <EmailLink href={dashboardUrl}>{dashboardUrl}</EmailLink> </EmailLayout> ); } WelcomeMail.PreviewProps = { userName: "Ada", appName: "Ledger", supportMail: "help@ledger.dev", dashboardUrl: "https://ledger.dev/dashboard", } satisfies WelcomeMailProps; src/mail/templates/reset-password-mail.tsx — the same components, different content:
import { Section, Text } from "react-email"; import { EmailButton, EmailHeading, EmailInfoCard, EmailLayout, } from "@/mail/components"; export interface ResetPasswordMailProps { userName: string; resetUrl: string; appName: string; supportMail: string; ipAddress?: string; } export default function ResetPasswordMail({ userName, resetUrl, appName, supportMail, ipAddress, }: ResetPasswordMailProps) { return ( <EmailLayout appName={appName} previewText="Reset your password" supportMail={supportMail} > <EmailHeading>Reset your password</EmailHeading> <Text>Hello {userName},</Text> <Text>We received a request to reset your {appName} password.</Text> <Section className="my-6 text-center"> <EmailButton href={resetUrl}>Choose a new password</EmailButton> </Section> {ipAddress ? ( <EmailInfoCard> <Text className="m-0 text-xs text-muted"> Request came from IP <span className="font-mono">{ipAddress}</span>. </Text> </EmailInfoCard> ) : null} <Text className="text-sm text-muted"> This link expires in one hour. If you did not ask for it, ignore this email and your password stays the same. </Text> </EmailLayout> ); } src/mail/templates/index.ts:
export { default as WelcomeMail, type WelcomeMailProps } from "./welcome-mail"; export { default as ResetPasswordMail, type ResetPasswordMailProps, } from "./reset-password-mail"; The rest of the family is the same layout with different props. Nothing below needs a new component, only a new heading, a new call to action, and the fields the mail must report:
| Template | Props besides userName, appName, supportMail | Uses |
|---|---|---|
EmailVerificationMail | verifyUrl, userAgent?, ipAddress? | EmailButton, EmailInfoCard, EmailLink |
PasswordChangedMail | changeTimestamp, ipAddress, deviceInfo | EmailButton, EmailInfoCard |
NewDeviceLoginMail | loginTimestamp, deviceInfo, browser, ipAddress, approximateLocation, secureAccountUrl | Row/Column inside EmailInfoCard |
SuspiciousLoginMail | attemptTimestamp, ipAddress, location, deviceInfo, secureAccountUrl | EmailButton, EmailInfoCard |
AccountLockedMail | lockTimestamp, failedAttempts, ipAddress, unlockUrl | EmailButton, EmailInfoCard |
RoleChangedMail | oldRole, newRole, changedBy, permissionsUrl | Row/Column inside EmailInfoCard |
Every one of them takes userName, appName, and supportMail. That is correct at the component level — the layout cannot guess them — but it is a bad prop surface for a call site: eight senders, eight chances to pass the wrong app name or forget the support address. The service below takes the shared ones once and hides them.
Import the shared layout and templates through the alias. This article uses @/, which maps to src/; swap in whatever alias your project uses. If you have no alias, use relative paths.
Pattern 1: Serve templates through one service, not through call sites
Email needs an HTML part and a plain-text part, so every send has to render one tree twice over. The layout also needs appName and supportMail, and no caller should have to know that. Both problems have the same fix: put them in a config object, subtract them from the public option type, and render every template in one place.
src/mail/mail.types.ts — transport options plus the template's props, minus the ones the service fills:
import type { SendMailOption } from "@/mail/transport/mail-transport.types"; export interface MailConfig { appName: string; systemMail: string; supportMail: string; } /** Transport options plus the template's props, minus the ones the service fills. */ export type MailTemplateOptions<TProps> = Omit< SendMailOption, "from" | "subject" | "text" | "html" > & Omit<TProps, "appName" | "supportMail">; export interface QueueMailResult { success: boolean; emailId?: string; error?: string; } /** The layer that persists and queues a rendered mail. */ export interface MailQueue { sendMail: ( options: SendMailOption, isSystemMail?: boolean ) => Promise<QueueMailResult>; } The Omit<TProps, "appName" | "supportMail"> is the whole trick. MailTemplateOptions<WelcomeMailProps> still requires userName and dashboardUrl, still forbids a typo, and no longer offers a field the caller could get wrong. If someone passes appName anyway, it is an excess-property error, not a silently ignored prop.
src/mail/mail-service.tsx — one render path, two templates:
import { render, toPlainText } from "react-email"; import { APP_NAME } from "@/constants"; import type { MailConfig, MailQueue, MailTemplateOptions, QueueMailResult, } from "@/mail/mail.types"; import { ResetPasswordMail, type ResetPasswordMailProps, WelcomeMail, type WelcomeMailProps, } from "@/mail/templates"; import type { SendMailOption } from "@/mail/transport/mail-transport.types"; export interface MailServiceConfig { mailConfig: MailConfig; queue: MailQueue; } export class MailService { private readonly mailConfig: MailConfig; private readonly queue: MailQueue; constructor({ mailConfig, queue }: MailServiceConfig) { this.mailConfig = mailConfig; this.queue = Source:https://dev.to/saiful7778/tailwind-emails-that-survive-the-inbox-react-email-without-the-guesswork-3c70 Related posts
Zero-ASAR-Patch Native UI Localization (14 Locales) for Google Antigravity on macOS, Windows & Linux
NIVOLA COFFEE TABLE
How I Built a Quiz Slayer App for My Class at Superior University
Protect an important panel without changing your scroll-snap track

Account Tile Tabs: the small motion details that make it feel right
