Skip to content

Email Templates

The Konigle SDK provides email template management for creating reusable email content with variable placeholders. Templates use Jinja2 format and support dynamic content insertion with built-in context like unsubscribe_link for single recipient emails.

Creating Email Templates

Basic Email Template

import konigle
from konigle.models.comm import EmailTemplateCreate

client = konigle.Client(api_key="your-api-key")

# Create a basic email template
template_data = EmailTemplateCreate(
    name="Welcome Email",
    code="welcome-email",
    subject="Welcome to {{company_name}}!",
    body_html="<h1>Welcome {{name}}!</h1><p>Thanks for joining us.</p>",
    body_text="Welcome {{name}}! Thanks for joining us.",
    tags=["welcome", "onboarding"]
)

template = client.email_templates.create(template_data)
print(f"Created template: {template.id}")
print(f"Template name: {template.name}")
print(f"Template code: {template.code}")

Base Template

You can mark a template as a base template for the account using the is_base field. Base templates serve as the foundation for email layouts and branding:

# Create a base template for the account
base_template_data = EmailTemplateCreate(
    name="Base Email Layout",
    code="base-layout",
    subject="{{subject}}",
    body_html="""
    <!DOCTYPE html>
    <html>
    <head>
        <style>
            .header { background-color: #4CAF50; padding: 20px; }
            .content { padding: 20px; }
            .footer { background-color: #f1f1f1; padding: 10px; }
        </style>
    </head>
    <body>
        <div class="header">
            <h1>Your Company Name</h1>
        </div>
        <div class="content">
            {{content}}
        </div>
        <div class="footer">
            <p>&copy; 2024 Your Company. All rights reserved.</p>
        </div>
    </body>
    </html>
    """,
    body_text="{{content}}",
    tags=["base", "layout"],
    is_base=True
)

base_template = client.email_templates.create(base_template_data)
print(f"Created base template: {base_template.id}")
print(f"Is base: {base_template.is_base}")

Listing Email Templates

from konigle.filters.comm import EmailTemplateFilters

# List all templates with pagination
templates = client.email_templates.list(page=1, page_size=20)

print(f"Total templates: {templates.count}")
for template in templates.payload:
    print(f"- {template.name} (Code: {template.code})")
    print(f"  Tags: {', '.join(template.tags)}")
    print(f"  Is base: {template.is_base}")

# List with search and filtering
filters = EmailTemplateFilters(
    q="welcome",  # Search in name, code, and tags
    tags="onboarding",  # Filter by tags
    ordering="-created_at"  # Order by creation date descending
)

filtered_templates = client.email_templates.list(
    page=1,
    page_size=10,
    filters=filters
)

for template in filtered_templates.payload:
    print(f"Found template: {template.name}")

# List only base templates
base_filters = EmailTemplateFilters(
    is_base=True  # Filter for base templates only
)

base_templates = client.email_templates.list(filters=base_filters)
print(f"Found {base_templates.count} base templates")
for template in base_templates.payload:
    print(f"- {template.name}")

Getting a Specific Template

# Get template by ID
template = client.email_templates.get("template_123456789")
print(f"Template: {template.name}")
print(f"Code: {template.code}")
print(f"Subject: {template.subject}")
print(f"HTML Body: {template.body_html}")
print(f"Text Body: {template.body_text}")
print(f"Tags: {template.tags}")
print(f"Created: {template.created_at}")

Updating Email Templates

from konigle.models.comm import EmailTemplateUpdate

# Update template information
update_data = EmailTemplateUpdate(
    name="Updated Welcome Email",
    subject="Welcome to {{company_name}}, {{name}}!",
    body_html="<h1>Hello {{name}}!</h1><p>Welcome to {{company_name}}.</p>",
    tags=["welcome", "onboarding", "personalized"]
)

updated_template = client.email_templates.update("template_123456789", update_data)
print(f"Updated template: {updated_template.name}")

# Update specific fields only
partial_update = EmailTemplateUpdate(
    name="New Template Name"
)
template = client.email_templates.update("template_123456789", partial_update)

# Mark a template as base template
mark_as_base = EmailTemplateUpdate(
    is_base=True
)
base_template = client.email_templates.update("template_123456789", mark_as_base)
print(f"Template is now base: {base_template.is_base}")

# Unmark a template as base template
unmark_as_base = EmailTemplateUpdate(
    is_base=False
)
regular_template = client.email_templates.update("template_123456789", unmark_as_base)
print(f"Template is base: {regular_template.is_base}")

Deleting Email Templates

# Delete an email template
success = client.email_templates.delete("template_123456789")
if success:
    print("Template deleted successfully")
else:
    print("Failed to delete template")

Async Operations

import asyncio
import konigle

async def manage_email_templates():
    async with konigle.AsyncClient(api_key="your-api-key") as client:
        # Create template
        template_data = EmailTemplateCreate(
            name="Async Template",
            code="async-template",
            subject="Async Email {{name}}",
            body_html="<h1>Hello {{name}}</h1>",
            tags=["async"]
        )
        template = await client.email_templates.create(template_data)

        # List templates with filtering
        filters = EmailTemplateFilters(q="async")
        templates = await client.email_templates.list(filters=filters)

        # Update template
        update_data = EmailTemplateUpdate(name="Updated Async Template")
        updated_template = await client.email_templates.update(template.id, update_data)

        # Delete template
        await client.email_templates.delete(template.id)

asyncio.run(manage_email_templates())

CLI Commands

The Konigle CLI provides comprehensive email template management commands.

Template Creation

# Create a basic email template
konigle comm email templates create \
    --name "Welcome Email" \
    --code "welcome-email" \
    --subject "Welcome to {{company_name}}!" \
    --body-html "<h1>Welcome {{name}}!</h1><p>Thanks for joining us.</p>" \
    --body-text "Welcome {{name}}! Thanks for joining us." \
    --tag "welcome" \
    --tag "onboarding"

# Create minimal template
konigle comm email templates create \
    --name "Simple Newsletter" \
    --code "newsletter" \
    --subject "Weekly Update" \
    --body-html "<h2>This Week's News</h2><p>Stay updated!</p>"

# Create a base template
konigle comm email templates create \
    --name "Base Email Layout" \
    --code "base-layout" \
    --subject "{{subject}}" \
    --body-html "<div class='header'><h1>Company</h1></div><div>{{content}}</div>" \
    --tag "base" \
    --tag "layout" \
    --is-base

Template Listing

# List all templates
konigle comm email templates list

# List with pagination
konigle comm email templates list --page 2 --page-size 5

# List with search
konigle comm email templates list --search "welcome"

# List with tag filtering
konigle comm email templates list --tags "onboarding,welcome"

# List only base templates
konigle comm email templates list --is-base true

# List only non-base templates
konigle comm email templates list --is-base false

# List with ordering
konigle comm email templates list --ordering "-created_at"

Template Details

# Get specific template details
konigle comm email templates get template_123456789

Template Updates

# Update template name
konigle comm email templates update template_123456789 --name "New Template Name"

# Update template content
konigle comm email templates update template_123456789 \
    --subject "New Subject {{name}}" \
    --body-html "<h1>New Content {{name}}</h1>"

# Update tags
konigle comm email templates update template_123456789 \
    --tag "updated" \
    --tag "v2"

# Mark template as base template
konigle comm email templates update template_123456789 --is-base

# Unmark template as base template
konigle comm email templates update template_123456789 --no-is-base

Template Deletion

# Delete template with confirmation
konigle comm email templates delete template_123456789

# Delete template without confirmation
konigle comm email templates delete template_123456789 --yes