@shopnex/easy-email-plugin
Version:
Visual email template editor plugin for Payload CMS with drag-and-drop interface
273 lines (196 loc) ⢠7.83 kB
Markdown
# Easy Email Plugin for Payload CMS
A powerful visual email template editor plugin for Payload CMS that provides an intuitive drag-and-drop interface for creating and managing email templates. This plugin integrates with a separate email editor app (shopnex-email) that runs as an iframe.
## ⨠Features
- š§ **Visual Email Editor** ā Drag-and-drop interface for building email templates
- šØ **Rich Components** ā Pre-built email components (text, images, buttons, etc.)
- š± **Responsive Design** ā Create mobile-friendly email templates
- š¾ **Template Management** ā Store templates as JSON and HTML
- š **Real-time Preview** ā See changes instantly as you build
- š **Easy Integration** ā Simple plugin setup with Payload CMS
- šÆ **Type Safe** ā Full TypeScript support
- š¼ļø **Iframe-based** ā Editor runs in a separate app for better isolation
## š Prerequisites
This plugin requires the **shopnex-email** app to be running. The email editor runs as a separate Next.js application that is embedded via iframe.
### Setting up the Email Editor App
1. **Navigate to the shopnex-email app:**
```bash
cd apps/shopnex-email
```
2. **Install dependencies:**
```bash
pnpm install
```
3. **Configure environment variables:**
```bash
cp .env.example .env
```
Edit `.env` and set your Payload CMS server URL:
```env
NEXT_PUBLIC_SERVER_URL=http://localhost:3000
```
4. **Start the email editor app:**
```bash
pnpm dev
```
The app will run on `http://localhost:3040` by default.
5. **Deploy for production** (optional):
The email editor app can be deployed separately to any static hosting:
- Cloudflare Pages
- Vercel
- Netlify
```bash
pnpm build
```
## š Installation
Install the plugin in your Payload CMS project:
```bash
npm install @shopnex/easy-email-plugin
# or
pnpm add @shopnex/easy-email-plugin
# or
yarn add @shopnex/easy-email-plugin
```
## š ļø Basic Usage
Add the plugin to your Payload configuration:
```ts
import { easyEmailPlugin } from "@shopnex/easy-email-plugin";
export default buildConfig({
plugins: [
easyEmailPlugin({
enabled: true,
}),
// ... other plugins
],
// ... rest of config
});
```
## š§ Configuration
### Plugin Options
```ts
interface EmailChannelPluginConfig {
enabled?: boolean;
collectionOverrides?: Partial<CollectionConfig>;
}
```
### Advanced Configuration
```ts
import { easyEmailPlugin } from "@shopnex/easy-email-plugin";
export default buildConfig({
plugins: [
easyEmailPlugin({
enabled: true,
collectionOverrides: {
slug: "custom-email-templates",
admin: {
group: "Marketing",
defaultColumns: ["name", "subject", "updatedAt"],
},
fields: [
{
name: "subject",
type: "text",
required: true,
},
// Add custom fields here
],
},
}),
],
});
```
## š Collection Structure
The plugin automatically creates an `email-templates` collection with the following fields:
- **name** (text) ā Template name
- **html** (textarea) ā Generated HTML output (auto-generated, disabled for editing)
- **json** (json) ā Template JSON structure for the editor
## šØ Email Template Editor
The plugin provides a custom edit view with a visual email editor interface:
1. Navigate to the Email Templates collection in your Payload admin panel
2. Click "Create New" to start building a new template
3. Use the drag-and-drop interface to add and configure email components
4. Preview your email template in real-time
5. Save to generate both JSON and HTML versions
## š Environment Variables
Configure the iframe origin for the email editor in your Payload CMS project:
```env
# URL for the email editor iframe (required)
# In development:
EASY_EMAIL_IFRAME_ORIGIN=http://localhost:3040
# In production (use your deployed email editor URL):
EASY_EMAIL_IFRAME_ORIGIN=https://email-editor.yourdomain.com
```
**Important:** Make sure the `EASY_EMAIL_IFRAME_ORIGIN` matches the URL where your shopnex-email app is running.
## š Client Components
The plugin exports client components for custom integrations:
```ts
import { EmailTemplate } from "@shopnex/easy-email-plugin/client";
// Use in your custom views
<EmailTemplate
html={emailHtml}
json={templateJson}
serverURL={payload.config.serverURL}
templateName="My Template"
identifier="template-id"
token={authToken}
iframeOrigin="http://localhost:3040"
/>
```
## š¦ Package Exports
```ts
// Main plugin export
import { easyEmailPlugin } from "@shopnex/easy-email-plugin";
// Client components (use in admin UI)
import { EmailTemplate, EmailTemplateEditView } from "@shopnex/easy-email-plugin/client";
// Server components (use in backend)
import { ... } from "@shopnex/easy-email-plugin/rsc";
```
## šÆ Use Cases
- **Transactional Emails** ā Order confirmations, shipping notifications
- **Marketing Campaigns** ā Newsletters, promotional emails
- **User Communications** ā Welcome emails, password resets
- **Event Notifications** ā Reminders, updates, alerts
## šļø Architecture
This plugin follows a decoupled architecture:
```
āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā
ā Payload CMS ā ā shopnex-email ā
ā (Backend) ā ā (Email Editor UI) ā
ā ā ā ā
ā - Plugin āāāāāāāāāāŗā - Next.js App ā
ā - Email Templates ā iframe ā - Easy Email UI ā
ā - Collection ā ā - Client-side only ā
āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā
```
**Benefits:**
- š **Better Security** ā Editor runs in isolated iframe
- š **Faster Loading** ā Editor can be cached separately
- š¦ **Smaller Bundle** ā Email editor code not included in main app
- š **Flexible Deployment** ā Deploy editor to CDN/static hosting
## š Security
The plugin uses Payload's built-in encryption for authentication tokens when communicating with the email editor iframe. All template data is stored securely in your Payload database.
## š§ Troubleshooting
### Email editor not loading
1. Ensure the shopnex-email app is running on the configured port (default: 3040)
2. Check that `EASY_EMAIL_IFRAME_ORIGIN` environment variable is set correctly
3. Verify the iframe origin matches the actual URL of your email editor app
### Connection errors
1. Make sure `NEXT_PUBLIC_SERVER_URL` in shopnex-email/.env points to your Payload CMS server
2. Check for CORS issues if running on different domains in production
3. Ensure your Payload CMS server is accessible from the email editor app
### Templates not saving
1. Verify database connection in Payload CMS
2. Check browser console for errors
3. Ensure the email-templates collection was created successfully
## š¤ Contributing
Contributions are welcome! Please read our contributing guidelines and submit pull requests to the main repository.
## š License
MIT ā Ā© 2025 ShopNex.ai
## š Links
- [Homepage](https://shopnex.ai)
- [Repository](https://github.com/shopnex-ai/shopnex)
- [Documentation](https://shopnex.ai/docs)
- [Issues](https://github.com/shopnex-ai/shopnex/issues)
## šØāš» Author
**Arseniy** ā arseniy@shopnex.ai
---
Made with ā¤ļø by the ShopNex.ai team