Custom Email Templates
Svelte Guardian sends various emails for authentication flows such as email verification, password reset, and two-factor authentication. This guide shows you how to customize these email templates to match your application’s branding and requirements.
Default Email Templates
By default, Svelte Guardian provides simple but functional email templates for:
- Email verification
- Password reset
- Two-factor authentication codes
- Welcome emails
- Security notifications
These templates include your application’s name, necessary links or codes, and brief instructions.
Customization Options
There are several ways to customize email templates in Svelte Guardian:
- Basic Configuration: Simple text and branding changes
- Custom HTML Templates: Complete control over email design
- Dynamic Templates: Templates with custom variables and logic
- Template Providers: Integration with email services like SendGrid, Mailjet, etc.
Basic Configuration
For simple customization, you can modify text and branding in your Svelte Guardian configuration:
This will customize the default templates with your branding elements.
Custom HTML Templates
For more control, provide your own HTML templates:
HTML templates can use variables with the {{variable}}
syntax.
Example HTML Template
Here’s an example verification email template (./src/emails/verification.html
):
Verify your email address
Hello {{name}},
Thank you for signing up for {{appName}}. To complete your registration, please verify your email address.
{{#if otpMethod}}Your verification code is:
Enter this code on the verification page to confirm your email address.
{{else}}Click the button below to verify your email address:
Verify EmailIf the button doesn't work, you can also copy and paste this link into your browser:
{{verificationUrl}}
{{/if}}This {{#if otpMethod}}code{{else}}link{{/if}} will expire in {{expirationMinutes}} minutes.
If you didn't create an account with us, you can safely ignore this email.
Available Template Variables
Variable | Description | Example |
---|---|---|
{{appName}} | Your application name | “Svelte Guardian” |
{{appUrl}} | Your application URL | ”https://example.com” |
{{logo}} | URL to your logo | ”https://example.com/logo.png” |
{{primaryColor}} | Your brand color | “#4F46E5” |
{{name}} | User’s name | “John Doe” |
{{email}} | User’s email | ”john@example.com” |
{{otp}} | Verification/reset code | “123456” |
{{verificationUrl}} | Full verification URL | ”https://example.com/verify?token=abc123” |
{{resetUrl}} | Full password reset URL | ”https://example.com/reset?token=abc123” |
{{expirationMinutes}} | Expiration time in minutes | “15” |
{{footerText}} | Custom footer text | ”© 2025 Your Company” |
Dynamic Templates with Template Functions
For more complex templates with logic, provide template functions instead of static files:
Welcome to ${appName}, ${user.name || 'there'}!
Your verification code is: ${otp}
`; }, text: (data) => { // Plain text version return `Verify your ${data.appName} account Your code: ${data.otp}`; } } } } } });Using Template Providers
For advanced email needs, you can integrate with template providers like SendGrid, Mailjet, or Mailchimp:
Testing Email Templates
To test your email templates during development:
- Enable console logging for emails:
- Or use a development email service like Mailhog or Ethereal:
Localization
For multi-language support, use template functions with translations:
Best Practices for Email Templates
- Always include plain text versions for better deliverability and accessibility
- Keep your design responsive for mobile devices
- Test across email clients (Gmail, Outlook, Apple Mail, etc.)
- Use inline CSS as many email clients strip
<style>
tags - Keep emails simple and focused on a single call to action
- Include your company’s physical address to comply with anti-spam laws
- Provide clear unsubscribe options for marketing emails