Framework.Effects.Email (Framework v0.5.0)

View Source

Email sending and template rendering for Framework effects.

Provides email sending capabilities with template rendering support. Uses Swoosh for email delivery with configurable adapters.

Configuration

Configure in your application config:

config :framework, :email_config, %{
  adapter: Swoosh.Adapters.SMTP,  # or Swoosh.Adapters.Test for testing
  from_email: "noreply@yourapp.com",
  from_name: "Your App",
  templates_dir: "priv/email_templates",
  # Adapter-specific config
  smtp: %{
    relay: "smtp.yourprovider.com",
    username: "your_username",
    password: "your_password",
    tls: :always,
    auth: :always,
    port: 587
  }
}

Template System

Email templates are simple text files with variable interpolation:

  • Subject templates: templates_dir/<template_name>_subject.txt
  • Body templates: templates_dir/<template_name>_body.txt
  • HTML templates (optional): templates_dir/<template_name>_body.html

Variables in templates are replaced using {{variable_name}} syntax.

Summary

Functions

Get the configured email settings.

Render an email template with the given model variables.

Send an email using the framework's email configuration.

Functions

get_email_config()

Get the configured email settings.

render_template(template, type, model, config, opts \\ [])

Render an email template with the given model variables.

Templates use {{variable_name}} syntax for variable interpolation.

Locale-aware Template Resolution

Templates are searched in order:

  1. Locale-specific: templates_dir/{locale}/{template}_{type}.txt
  2. Default: templates_dir/{template}_{type}.txt

Example:

  • Locale "es-ES": priv/email_templates/es-ES/welcome_subject.txt
  • Fallback: priv/email_templates/welcome_subject.txt

send_email(template, to, model, opts \\ [])

Send an email using the framework's email configuration.

Parameters

  • template: Template name (e.g., "welcome", "password_reset")
  • to: Recipient email address
  • model: Variables for template rendering
  • opts: Additional options
    • :external_id - Idempotency key for logging
    • :correlation_ids - Request correlation context

Returns

  • :ok on successful send
  • {:error, reason} on failure