Minimal setup for Plug.Swoosh.MailboxPreview?

Hi!

I’m building a small internal service to handle emails (it is not a webserver) and for this I have choosen Swoosh. Now I am trying to setup the mailbox preview and I want to use as few dependencies and as minimal setup as possible. Idealy, I would not even need to include it in the production environment but only in dev. What is hte minimal setup to make the preview work? Do I need to use pheonix as a dependency or is plug enough?

This is what I am currently trying:

config:

config :muninn, Phoenix.Endpoint,
  url: [host: "localhost"],
  render_errors: [formats: [html: Phoenix.Template.HTML]],
  pubsub_server: Muninn.PubSub,
  adapter: Bandit.PhoenixAdapter,
  live_view: [signing_salt: "some_salt"]

application:

defmodule Muninn.Application do
  @moduledoc """
  Application module for Muninn.
  """

  use Application
  require Logger
  
  @impl true
  def start(_type, _args) do
    # Oban.Telemetry.attach_default_logger(
    #   level: Application.get_env(:logger, :level),
    #   encode: true
    # )

    children = [
      Muninn.Repo,
      {Oban, Application.fetch_env!(:muninn, Oban)}
    ]

    children =
      if Application.get_env(:muninn, :email_preview) do
        # children ++ [{Phoenix.Endpoint, []}]
        children ++
          [
            # {Plug.Cowboy, scheme: :http, plug: Muninn.Router, options: [port: 4000]}
            {Phoenix.PubSub, name: Muninn.PubSub},
            # Start to serve requests, typically the last entry
            Muninn.Endpoint
          ]
      end

    Supervisor.start_link(children, strategy: :one_for_one)
  end

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  @impl true
  def config_change(changed, _new, removed) do
    Muninn.Endpoint.config_change(changed, removed)
    :ok
  end
end

Endpoint:

defmodule Muninn.Endpoint do
  use Phoenix.Endpoint, otp_app: :runar

  # The session will be stored in the cookie and signed,
  # this means its contents can be read but not tampered with.
  # Set :encryption_salt if you would also like to encrypt it.
  @session_options [
    store: :cookie,
    key: "_muninn_key",
    signing_salt: "C6nzbDmG",
    same_site: "Lax"
  ]

  socket("/live", Phoenix.LiveView.Socket,
    websocket: [connect_info: [:peer_data, session: @session_options]],
    longpoll: [connect_info: [:peer_data, session: @session_options]]
  )

  # Serve at "/" the static files from "priv/static" directory.
  #
  # You should set gzip to true if you are running phx.digest
  # when deploying your static files in production.
  plug(Plug.Static,
    at: "/",
    from: :muninn,
    gzip: false,
    only: ~w(assets fonts images favicon.ico robots.txt manifest.json)
  )

  # Code reloading can be explicitly enabled under the
  # :code_reloader configuration of your endpoint.
  if code_reloading? do
    socket("/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket)
    plug(Phoenix.LiveReloader)
    plug(Phoenix.CodeReloader)
    # plug(Phoenix.Ecto.CheckRepoStatus, otp_app: :muninn)
  end

  plug(Plug.RequestId)
  # plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])

  plug(Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Phoenix.json_library()
  )

  plug(Plug.MethodOverride)
  plug(Plug.Head)
  plug(Plug.Session, @session_options)
  plug(Muninn.Router)
end

router file:

defmodule Muninn.Router do
  use Phoenix.Router
  import Plug.Conn
  import Phoenix.Controller
  import Phoenix.LiveReloader

  pipeline :browser do
    plug(:accepts, ["html"])
    plug(:fetch_session)

    if Mix.env() == :dev do
      plug(:fetch_live_reload)
    end
  end

  scope "/dev" do
    pipe_through(:browser)

    forward("/mailbox", Plug.Swoosh.MailboxPreview,
      csp_nonce_assign_key: %{script: :script_csp_nonce, style: :style_csp_nonce}
    )
  end
end

But I’m getting this error:

error: undefined function fetch_live_reload/2 (expected Muninn.Router to define such a function or for it to be imported, but none are available)
└─ deps/phoenix/lib/phoenix/router.ex: Muninn.Router.browser/2
```

Got it working by following the instructions in the README: GitHub - swoosh/swoosh: Compose, deliver and test your emails easily in Elixir