Skip to main content

Overview

The emailList() function scans your project directory and returns a list of all Svelte email component files. Use this function in your SvelteKit load functions to populate the email preview interface.

Function Signature

function emailList(options?: EmailListProps): PreviewData

Parameters

options
EmailListProps
default:"{}"
Optional configuration object
options.path
string
default:"'/src/lib/emails'"
Relative path from root to your emails folder
options.root
string
Absolute path to your project root. Auto-detected using process.cwd() if not provided

Return Type

PreviewData
object
Object containing the list of email files and the path
files
string[] | null
Array of relative file paths to email components (without .svelte extension), or null if directory doesn’t exist or is empty
path
string | null
The email directory path that was scanned

Usage

Basic Example

+page.server.ts
import { emailList } from 'better-svelte-email/preview';

export function load() {
  const emails = emailList();
  return { emails };
}

Custom Directory

+page.server.ts
import { emailList } from 'better-svelte-email/preview';

export function load() {
  const emails = emailList({
    root: process.cwd(),
    path: '/src/lib/emails'
  });
  return { emails };
}

Example Response

{
  "files": [
    "welcome",
    "password-reset",
    "notifications/daily-digest"
  ],
  "path": "/src/lib/emails"
}
The function automatically handles nested directories and returns relative paths. File names are returned without the .svelte extension.

Behavior

Directory Not Found

If the email directory doesn’t exist, the function returns:
{
  "files": null,
  "path": "/src/lib/emails"
}
A warning is logged to the console:
Email directory not found: /absolute/path/to/emails

Empty Directory

If no .svelte files are found, the function returns:
{
  "files": null,
  "path": "/src/lib/emails"
}

Root Path Detection

If root is not provided, the function attempts to use process.cwd(). If this fails, an error is thrown:
In some environments (like edge runtimes), process.cwd() may not be available. In these cases, you must explicitly pass the root parameter.

Integration with EmailPreview Component

This function is typically used together with the EmailPreview component:
+page.server.ts
import { emailList, createEmail, sendEmail } from 'better-svelte-email/preview';
import { PRIVATE_RESEND_API_KEY } from '$env/static/private';

export function load() {
  const emails = emailList();
  return { emails };
}

export const actions = {
  ...createEmail(),
  ...sendEmail({ resendApiKey: PRIVATE_RESEND_API_KEY })
};
+page.svelte
<script>
  import { EmailPreview } from 'better-svelte-email/preview';
  
  export let data;
</script>

<EmailPreview {data} />

Build docs developers (and LLMs) love