Search documentation of phoenix_kit

# PhoenixKit.Emails.Template (phoenix_kit v1.6.19)

View Source

Email template schema for managing reusable email templates.

This module defines the structure and validations for email templates that can be used throughout the application. Templates support variable substitution and categorization for better organization.

## Template Variables

Templates support variable substitution using the `{{variable_name}}` syntax. Common variables include:

- `{{email}}` - User's email address
- `{{url}}` - Action URL (magic link, confirmation, etc.)
- `{{timestamp}}` - Current timestamp
- `{{user_name}}` - User's display name

## Categories

- system - Core authentication and system emails (protected)
- marketing - Promotional and marketing communications
- transactional - Order confirmations, notifications, etc.

## Status

- active - Template is live and can be used
- draft - Template is being edited
- archived - Template is no longer active but preserved

## Examples

    # Create a new template
    %EmailTemplate{}
    |> EmailTemplate.changeset(%{
      name: "welcome_email",
      slug: "welcome-email",
      display_name: "Welcome Email",
      subject: "Welcome to {{app_name}}!",
      html_body: "<h1>Welcome {{user_name}}!</h1>",
      text_body: "Welcome {{user_name}}!",
      category: "transactional",
      status: "active"
    })

# Summary

## Types

t()

## Functions

changeset(template, attrs)

Creates a changeset for email template creation and updates.

common_variables()

Returns the list of common template variables.

extract_variables(template)

Extracts variables from template content (subject, html_body, text_body).

substitute_variables(template, variables)

Substitutes variables in template content with provided values.

usage_changeset(template, attrs \\ %{})

Creates a changeset for updating template usage statistics.

valid_categories()

Returns the list of valid categories for email templates.

valid_statuses()

Returns the list of valid statuses for email templates.

version_changeset(template, attrs \\ %{})

Creates a changeset for updating template version.

# Types

# t()

    @type t() :: %PhoenixKit.Emails.Template{
      __meta__: term(),
      category: String.t(),
      created_by_user_id: integer() | nil,
      description: String.t() | nil,
      display_name: String.t(),
      html_body: String.t(),
      id: integer() | nil,
      inserted_at: DateTime.t() | nil,
      is_system: boolean(),
      last_used_at: DateTime.t() | nil,
      metadata: map(),
      name: String.t(),
      slug: String.t(),
      status: String.t(),
      subject: String.t(),
      text_body: String.t(),
      updated_at: DateTime.t() | nil,
      updated_by_user_id: integer() | nil,
      usage_count: integer(),
      variables: map(),
      version: integer()
    }

# Functions

# changeset(template, attrs)

Creates a changeset for email template creation and updates.

## Parameters

- `template` - The email template struct (new or existing)
- `attrs` - Map of attributes to change

## Required Fields

- `:name` - Unique template identifier
- `:slug` - URL-friendly identifier
- `:display_name` - Human-readable name
- `:subject` - Email subject line
- `:html_body` - HTML version of email
- `:text_body` - Plain text version of email

## Validations

- Name must be unique and follow snake_case format
- Slug must be unique and URL-friendly
- Category must be one of the valid categories
- Status must be one of the valid statuses
- Subject and body fields cannot be empty
- Variables must be a valid map

# common_variables()

Returns the list of common template variables.

# extract_variables(template)

Extracts variables from template content (subject, html_body, text_body).

Returns a list of unique variable names found in the template.

## Examples

    iex> template = %EmailTemplate{
    ...>   subject: "Welcome {{user_name}}!",
    ...>   html_body: "<p>Hi {{user_name}}, click {{url}}</p>",
    ...>   text_body: "Hi {{user_name}}, visit {{url}}"
    ...> }
    iex> EmailTemplate.extract_variables(template)
    ["user_name", "url"]

# substitute_variables(template, variables)

Substitutes variables in template content with provided values.

## Parameters

- `template` - The email template
- `variables` - Map of variable names to values

Returns a new template struct with variables substituted.

## Examples

    iex> template = %EmailTemplate{
    ...>   subject: "Welcome {{user_name}}!",
    ...>   html_body: "<p>Hi {{user_name}}</p>"
    ...> }
    iex> result = EmailTemplate.substitute_variables(template, %{"user_name" => "John"})
    iex> result.subject
    "Welcome John!"

# usage_changeset(template, attrs \\ %{})

Creates a changeset for updating template usage statistics.

# valid_categories()

Returns the list of valid categories for email templates.

# valid_statuses()

Returns the list of valid statuses for email templates.

# version_changeset(template, attrs \\ %{})

Creates a changeset for updating template version.