Packages
Reusable structured audit logging, DB change tracking, and crash reporting for Elixir/Phoenix apps
Current section
Files
Jump to
Current section
Files
lib/audit_trail/adapters/smtp.ex
defmodule AuditTrail.Adapters.SMTP do
@moduledoc """
Raw SMTP adapter using Swoosh's SMTP adapter directly.
Does not require a host-app Mailer module — reads SMTP config from AuditTrail config.
Config:
config :audit_trail,
mailer_adapter: {AuditTrail.Adapters.SMTP, []},
from_email: {"MyApp Alerts", "alerts@myapp.com"},
smtp: [
relay: "smtp.gmail.com",
username: "alerts@myapp.com",
password: System.get_env("SMTP_PASSWORD"),
port: 587,
tls: :always,
auth: :always
]
"""
def deliver(%{to: to, subject: subject, body: body}) do
smtp_config = AuditTrail.Config.smtp_config()
from_email = AuditTrail.Config.from_email()
email =
Swoosh.Email.new()
|> Swoosh.Email.to(to)
|> Swoosh.Email.from(from_email)
|> Swoosh.Email.subject(subject)
|> Swoosh.Email.text_body(body)
Swoosh.Adapters.SMTP.deliver(email, smtp_config)
rescue
e ->
require Logger
Logger.error("[AuditTrail.Adapters.SMTP] Delivery failed: #{inspect(e)}")
{:error, e}
end
end