EmailPreview component provides a visual development environment for creating and testing your email templates. It includes features like live preview, HTML source and code source viewing, and test email sending—all within your SvelteKit application.
Quick start
See your email templates in action by visiting the live preview demo.Setup
Create a preview route
Create a new route at
src/routes/email-preview/[...email]/+page.svelte in your SvelteKit app:src/routes/email-preview/[...email]/+page.svelte
Configure server-side logic
Create a
+page.server.ts file in the same route to load email templates and handle preview actions:src/routes/email-preview/[...email]/+page.server.ts
Add your API key (optional)
To enable test email sending, add your Resend API key to your Get your API key from Resend.
.env file:.env
Configuration options
Custom email folder
By default, the preview looks for email templates in/src/lib/emails. Customize this path:
Custom root path
If your project structure requires it, specify a custom root path:Custom sender address
Change the default “from” address for test emails:Using with custom Tailwind config
To use your custom Tailwind configuration or app CSS in the preview, pass aRenderer instance:
Vercel deployment
Vercel’s serverless environment has limitations with dynamic imports. You’ll need to create custom serverless functions. See this example implementation for reference.Troubleshooting
No templates showing up
Make sure:- Your email templates are in the correct directory (default:
/src/lib/emails) - Your files have the
.svelteextension - The path in
emailList()matches your actual directory structure
Test emails not sending
Verify that:- Your
RESEND_API_KEYis set in your.envfile - The API key is valid and has sending permissions
- You’ve imported and spread the
sendEmailaction in your+page.server.ts
Preview route not found
Ensure:- You’ve created both
+page.svelteand+page.server.tsin your preview route - Your dev server is running
- You’re navigating to the correct URL (e.g.,
http://localhost:5173/email-preview)
API reference
emailList(options)
Loads all email templates from the specified directory. Parameters:options.path(string, optional) - Relative path from root to emails folder (default:/src/lib/emails)options.root(string, optional) - Absolute path to project root (auto-detected if not provided)
PreviewData object with:
files(string[] | null) - Array of email template file namespath(string | null) - Path to the emails directory
createEmail(options)
SvelteKit form action that renders an email component to HTML. Parameters:options.renderer(Renderer, optional) - Custom renderer instance with Tailwind config
sendEmail(options)
Returns a SvelteKit form action that sends test emails. Parameters:options.resendApiKey(string, optional) - Your Resend API keyoptions.customSendEmailFunction(function, optional) - Custom email sending functionoptions.renderer(Renderer, optional) - Custom renderer instance with Tailwind configoptions.from(string, optional) - Sender email address (defaults to'better-svelte-email <[email protected]>')