PhoenixKit.Emails.Templates (phoenix_kit v1.6.15)
View SourceContext module for managing email templates.
This module provides the business logic and database operations for email templates, including CRUD operations, template rendering, variable substitution, and usage tracking.
Main Functions
list_templates/1- List templates with filtering and paginationget_template/1- Get template by IDget_template_by_name/1- Get template by namecreate_template/1- Create a new templateupdate_template/2- Update existing templatedelete_template/1- Delete templaterender_template/2- Render template with variables
Examples
# List all active templates
Templates.list_templates(%{status: "active"})
# Get template by name
template = Templates.get_template_by_name("magic_link")
# Render template with variables
Templates.render_template(template, %{"user_name" => "John", "url" => "https://example.com"})
Summary
Functions
Activates an email template by setting its status to "active".
Archives an email template by setting its status to "archived".
Clones an existing template with a new name.
Returns the count of templates matching the given filters.
Creates a new email template.
Deletes an email template.
Gets an active template by name.
Gets a template by ID.
Gets a template by ID, raising an exception if not found.
Gets a template by name.
Gets template statistics for dashboard display.
Lists templates with optional filtering and pagination.
Returns the HTML template for magic link emails.
Returns the text template for magic link emails.
Returns the HTML template for registration confirmation emails.
Returns the text template for registration confirmation emails.
Renders a template with the provided variables.
Returns the HTML template for password reset emails.
Returns the text template for password reset emails.
Seeds the database with system email templates.
Sends an email using a template.
Returns the HTML template for test emails.
Returns the text template for test emails.
Increments the usage count for a template and updates last_used_at.
Returns the HTML template for email update confirmation emails.
Returns the text template for email update confirmation emails.
Updates an existing email template.
Functions
Activates an email template by setting its status to "active".
Examples
iex> Templates.activate_template(template)
{:ok, %Template{status: "active"}}
Archives an email template by setting its status to "archived".
Examples
iex> Templates.archive_template(template)
{:ok, %Template{status: "archived"}}
Clones an existing template with a new name.
Examples
iex> Templates.clone_template(template, "new_welcome_email")
{:ok, %Template{name: "new_welcome_email"}}
Returns the count of templates matching the given filters.
Creates a new email template.
Examples
iex> Templates.create_template(%{name: "welcome", subject: "Welcome!", ...})
{:ok, %Template{}}
iex> Templates.create_template(%{invalid: "data"})
{:error, %Ecto.Changeset{}}
Deletes an email template.
System templates (is_system: true) cannot be deleted.
Examples
iex> Templates.delete_template(template)
{:ok, %Template{}}
iex> Templates.delete_template(system_template)
{:error, :system_template_protected}
Gets an active template by name.
Only returns templates with status "active".
Examples
iex> Templates.get_active_template_by_name("magic_link")
%Template{}
Gets a template by ID.
Returns nil if the template does not exist.
Examples
iex> Templates.get_template(1)
%Template{}
iex> Templates.get_template(999)
nil
Gets a template by ID, raising an exception if not found.
Examples
iex> Templates.get_template!(1)
%Template{}
iex> Templates.get_template!(999)
** (Ecto.NoResultsError)
Gets a template by name.
Returns nil if the template does not exist.
Examples
iex> Templates.get_template_by_name("magic_link")
%Template{}
iex> Templates.get_template_by_name("nonexistent")
nil
Gets template statistics for dashboard display.
Returns a map with various statistics about templates.
Examples
iex> Templates.get_template_stats()
%{
total_templates: 10,
active_templates: 8,
draft_templates: 1,
archived_templates: 1,
system_templates: 4,
most_used: %Template{},
categories: %{"system" => 4, "transactional" => 6}
}
Lists templates with optional filtering and pagination.
Parameters
opts- Keyword list with filtering options::category- Filter by category ("system", "marketing", "transactional"):status- Filter by status ("active", "draft", "archived"):search- Search in name, display_name, or description:is_system- Filter by system templates (true/false):limit- Limit number of results:offset- Offset for pagination:order_by- Order by field (:name, :usage_count, :last_used_at, :inserted_at):order_direction- Order direction (:asc, :desc)
Examples
# List all templates
Templates.list_templates()
# List active marketing templates
Templates.list_templates(%{category: "marketing", status: "active"})
# Search templates
Templates.list_templates(%{search: "welcome"})
# Paginated results
Templates.list_templates(%{limit: 10, offset: 20})
Returns the HTML template for magic link emails.
Returns the text template for magic link emails.
Returns the HTML template for registration confirmation emails.
Returns the text template for registration confirmation emails.
Renders a template with the provided variables.
Returns a map with :subject, :html_body, and :text_body keys containing
the rendered content with variables substituted.
This function performs validation to ensure all template variables are properly substituted:
- Checks for missing required variables
- Warns if any unreplaced
{{variable}}placeholders remain - Logs information about unused variables
Examples
iex> Templates.render_template(template, %{"user_name" => "John"})
%{
subject: "Welcome John!",
html_body: "<h1>Welcome John!</h1>",
text_body: "Welcome John!"
}Validation
If required variables are missing or templates contain unreplaced variables, warnings will be logged but the function will still return the rendered content. This allows for graceful degradation in production.
Returns the HTML template for password reset emails.
Returns the text template for password reset emails.
Seeds the database with system email templates.
This function creates the default system templates for authentication and core functionality.
Examples
iex> Templates.seed_system_templates()
{:ok, [%Template{}, ...]}
Sends an email using a template.
This is a convenience wrapper around PhoenixKit.Mailer.send_from_template/4
that provides a cleaner API for sending templated emails.
Parameters
template_name- Name of the template (e.g., "welcome_email")recipient- Email address or {name, email} tuplevariables- Map of template variablesopts- Additional options (seePhoenixKit.Mailer.send_from_template/4)
Examples
# Send welcome email
Templates.send_email("welcome_email", user.email, %{
"user_name" => user.name,
"activation_url" => activation_url
})
# Send with tracking
Templates.send_email(
"order_confirmation",
customer.email,
%{"order_number" => order.number},
user_id: customer.id,
metadata: %{order_id: order.id}
)
Returns the HTML template for test emails.
Returns the text template for test emails.
Increments the usage count for a template and updates last_used_at.
This should be called whenever a template is used to send an email.
Examples
iex> Templates.track_usage(template)
{:ok, %Template{usage_count: 1}}
Returns the HTML template for email update confirmation emails.
Returns the text template for email update confirmation emails.
Updates an existing email template.
Examples
iex> Templates.update_template(template, %{subject: "New Subject"})
{:ok, %Template{}}
iex> Templates.update_template(template, %{invalid: "data"})
{:error, %Ecto.Changeset{}}