honungsburk

honungsburk

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
```

First Post!

honungsburk

honungsburk

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

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement