AshDispatch.EmailBackend.Swoosh (AshDispatch v0.5.0)

View Source

Swoosh-based email backend for AshDispatch.

Uses a configured Swoosh mailer to send emails. This is the production-ready email backend that integrates with any Swoosh adapter (Resend, SendGrid, Postmark, SMTP, etc.).

Configuration

Configure in your application's config:

# config/config.exs
config :ash_dispatch,
  email_backend: AshDispatch.EmailBackend.Swoosh,
  swoosh_mailer: MyApp.Mailer

Your Swoosh mailer should be configured separately:

# config/config.exs
config :my_app, MyApp.Mailer,
  adapter: Swoosh.Adapters.Resend,
  api_key: System.get_env("RESEND_API_KEY")

Testing

In tests, use Swoosh's test adapter:

# config/test.exs
config :my_app, MyApp.Mailer,
  adapter: Swoosh.Adapters.Test

Then use Swoosh.TestAssertions in your tests:

import Swoosh.TestAssertions

assert_email_sent(
  to: "user@example.com",
  subject: "Welcome!"
)

Return Value

Returns {:ok, metadata} on success with:

  • :id - Message ID from provider (if available)
  • :provider - Always :swoosh

Returns {:error, reason} on failure.

Example

AshDispatch.EmailBackend.Swoosh.send_email(%{
  to: "user@example.com",
  from: "orders@myapp.com",
  subject: "Order Confirmation",
  html_body: "<h1>Thank you!</h1>",
  text_body: "Thank you!"
})
# => {:ok, %{id: "msg_xyz", provider: :swoosh}}

Summary

Functions

Sends an email via Swoosh.

Functions

send_email(map)

@spec send_email(map()) :: {:ok, map()} | {:error, any()}

Sends an email via Swoosh.

Parameters

  • params - Map with:
    • :to - Recipient email address (string)
    • :from - Sender email address (string)
    • :subject - Email subject (string)
    • :html_body - HTML email body (string)
    • :text_body - Plain text email body (string)

Returns

  • {:ok, %{id: message_id, provider: :swoosh}} on success
  • {:error, reason} on failure

Examples

iex> send_email(%{
...>   to: "user@example.com",
...>   from: "app@example.com",
...>   subject: "Hello",
...>   html_body: "<p>Hi there</p>",
...>   text_body: "Hi there"
...> })
{:ok, %{id: "msg_123", provider: :swoosh}}