Overview
React Email allows us to:- Create email templates using React components
- Preview emails during development
- Ensure consistent styling across different email clients
- Send emails programmatically using services like Resend
lib/react-email/templates directory and can be previewed using the included preview server.
Directory Structure
Creating a New Email Template
To create a new email template:- Create a new file in the
lib/react-email/templatesdirectory - Use the React Email components and our shared styles
- Export the component as the default export
Available Components and Styles
Core Components
React Email provides these core components:<Html>,<Head>,<Body>- Structure components<Container>,<Section>- Layout components<Text>,<Heading>,<Hr>- Content components<Link>,<Button>- Interactive components<Img>- Image component
Custom Components
We’ve created these shared components:<Footer />- Consistent footer with social links and copyright<LogoSVG />- OnlyAutomator logo
Styles
Use the shared styles fromstyles/shared.ts:
styles.main- Email body stylestyles.container- Container stylestyles.box- Main content boxstyles.heading- Heading text stylestyles.paragraph- Paragraph text stylestyles.buttonContainer- Button container stylestyles.button- Primary button stylestyles.secondaryButton- Secondary button stylestyles.hr- Horizontal rule style
Previewing Email Templates
To preview the email templates during development:- Run the email preview server:
- Open your browser at
http://localhost:3001 - You’ll see a list of all available email templates
- Click on any template to preview it, with options to view in different email clients
scripts/email-preview.ts.
Sending Emails
To send emails using the templates, we use Resend through ourlib/resend.ts utility:
Environment Variables
The email templates use these environment variables (defined inemail-utils.tsx):
NEXT_PUBLIC_WEB_SITE_URL- Main website URLNEXT_PUBLIC_WEB_SITE_URL_DASHBOARD- Dashboard URLNEXT_PUBLIC_WEB_SITE_URL_CHROME_EXTENSION- Chrome extension URLNEXT_PUBLIC_WEB_SITE_URL_DOC- Documentation URLNEXT_PUBLIC_SUPPORT_URL- Support email/contact URLNEXT_PUBLIC_FEEDBACK_FORM_URL- Feedback form URL
Using Dynamic Content
Templates can include dynamic content like:- Conditional rendering:
- Looping over items:
- Dynamic styles based on props:
Best Practices
- Keep templates modular: Extract common elements into shared components
- Test on multiple clients: Use the preview tool to test on different email clients
- Use responsive design: Make sure emails look good on both desktop and mobile
- Use alt text: Always include descriptive alt text for images
- Avoid complex CSS: Email clients have limited CSS support
- Use semantic HTML: Use proper heading tags, paragraphs, etc.
- Provide text alternatives: Include plain text alternatives when possible
Troubleshooting
- Images not showing: Make sure image URLs are absolute and publicly accessible
- Styling issues: Some email clients may not support certain CSS properties; stick to basic styling
- Preview server not starting: Check that you have all necessary dependencies installed
- Send failures: Verify your Resend API key and email sending limits