Svelty-Email

0.1.1 · active · verified Tue Apr 21

Svelty-Email is a community-maintained fork of the svelte-email library, designed to facilitate the creation and rendering of email templates using Svelte. Inspired by React-Email, it allows developers to build responsive, component-based email layouts in a familiar Svelte environment, abstracting away the complexities of traditional HTML email development, such as table-based structures. The current stable version is 0.1.1, with a primary focus on adapting to Svelte's evolving ecosystem, including ongoing work for Svelte 5 support. Its key differentiator is enabling a modern Svelte development workflow for email templating, integrating seamlessly with SvelteKit projects and rendering emails to robust HTML or plain text suitable for various email service providers like Nodemailer. This project aims to keep the original library's vision alive and updated.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic email template using Svelte components provided by svelty-email and then render it to HTML for sending via Nodemailer in a SvelteKit context.

import { render } from 'svelty-email';
import Hello from '$lib/emails/Hello.svelte';
import nodemailer from 'nodemailer';

// src/$lib/emails/Hello.svelte
// <script>
// 	import { Button, Hr, Html, Text } from 'svelty-email';
// 	export let name = 'World';
// </script>
// <Html lang="en">
// 	<Text>
// 		Hello, {name}!
// 	</Text>
// 	<Hr />
// 	<Button href="https://svelte.dev">Visit Svelte</Button>
// </Html>

const transporter = nodemailer.createTransport({
	host: process.env.SMTP_HOST ?? 'smtp.ethereal.email',
	port: parseInt(process.env.SMTP_PORT ?? '587', 10),
	secure: process.env.SMTP_SECURE === 'true',
	auth: {
		user: process.env.SMTP_USER ?? 'my_user',
		pass: process.env.SMTP_PASSWORD ?? 'my_password'
	}
});

const emailHtml = render({
	template: Hello,
	props: {
		name: 'Svelte'
	}
});

const options = {
	from: 'you@example.com',
	to: 'user@gmail.com',
	subject: 'hello world',
	html: emailHtml
};

transporter.sendMail(options)
	.then(() => console.log('Email sent successfully!'))
	.catch(error => console.error('Failed to send email:', error));

// Example of usage in a SvelteKit server route (e.g., src/routes/send-email/+server.js)
// export async function POST() {
//   // ... (email sending logic as above)
//   return new Response('Email sent', { status: 200 });
// }

view raw JSON →