Function MyWeb.Bamboo.SentEmailViewerPlug.init/1 is undefined

I am working on an umbrella application that contains a separate Phoenix and Ecto application (my_web and my_app).

Then I Install the Bamboo email library for sending emails on the Phoenix App my_web. Bamboo can view the sent emails by using the Phoenix controller. I follow the configuration as described in the documentation.

# in config file (config/config.exs)
config: my_web, MyWeb.Mailer,
  adapter: Bamboo.LocalAdapter

# In Router
defmodule MyWeb.Router do

  if Mix.env ==: dev do
    # If using Phoenix
    forward "/ sent_emails", Bamboo.SentEmailViewerPlug

  end
end

However, there is an error message:
MyWeb.Bamboo.SentEmailViewerPlug.init / 1 function is undefined

It is wrong because the correct one is Bamboo.SentEmailViewerPlug module, not MyWeb.Bamboo.SentEmailViewerPlug.

Any ideas to resolve this are appreciated.

Thank you.

Take a look here if you haven’t https://hexdocs.pm/bamboo/Bamboo.LocalAdapter.html#content

This part

Example config

# In config/config.exs, or config/dev.exs, etc.
config :my_app, MyApp.Mailer,
  adapter: Bamboo.LocalAdapter,
  open_email_in_browser_url: "http://localhost:4000/sent_emails" # optional

# Define a Mailer. Maybe in lib/my_app/mailer.ex
defmodule MyApp.Mailer do
  use Bamboo.Mailer, otp_app: :my_app
end

Thank you for the response. I have read and follow the documentation before, and do as you suggested, but the error is still there.

OK, start the server with:

mix phx.server

Then navigate in the browser to http://localhost:4000/sent_emails

Then copy the full error and post if on the forum.

This might make it easier to understand what is going on.

This is the error

00:55:34.896 [error] #PID<0.941.0> running MyWeb.Endpoint (connection #PID<0.939.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /sent_emails
** (exit) an exception was raised:
    ** (UndefinedFunctionError) function MyWeb.Bamboo.SentEmailViewerPlug.init/1 is undefined (module MyWeb.Bamboo.SentEmailViewerPlug is not available)
        MyWeb.Bamboo.SentEmailViewerPlug.init([])
        (phoenix) lib/phoenix/router/route.ex:40: Phoenix.Router.Route.call/2
        (phoenix) lib/phoenix/router.ex:288: Phoenix.Router.__call__/2
        (my_web) lib/my_web/endpoint.ex:1: MyWeb.Endpoint.plug_builder_call/2
        (my_web) lib/plug/debugger.ex:122: MyWeb.Endpoint."call (overridable 3)"/2
        (my_web) lib/my_web/endpoint.ex:1: MyWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy) /Users/Hendri/GitHub/datingdev/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2

Can you please show us your full router.ex?

It might be that you have omitted something important…

Do you really have a space here? If so delete the space and write

forward "/sent_emails", Bamboo.SentEmailViewerPlug

Does it work now?

There is no space on the forward route. Here is the full router.ex

defmodule MyWeb.Router do
  use MyWeb, :router
  #use Phoenix.Router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", MyWeb do
    pipe_through [:browser, MyWeb.Plugs.Guest]

    get "/", FrontPageController, :index

    resources "/register", UserController, only: [:create, :new]
    get "/login", SessionController, :new
    post "/login", SessionController, :create

    get "/verify", UserController, :verify_email_from_user

    # Email Development route
    if Mix.env == :dev do
      forward "/sent_emails", Bamboo.SentEmailViewerPlug  
    end
  end

  scope "/", MyWeb do
    pipe_through [:browser, MyWeb.Plugs.Auth]

    delete "/logout", SessionController, :delete

    get "/page", PageController, :index

    resources "/user", UserController
  end

  # Other scopes may use custom stacks.
  # scope "/api", MyWeb do
  #   pipe_through :api
  # end
end

Just take it out of the scope. Move those 3 lines in your router.ex:

    if Mix.env == :dev do
      forward "/sent_emails", Bamboo.SentEmailViewerPlug  
    end

down one line (after the scope’s end) and I think it should be fine.

source: https://hexdocs.pm/bamboo/Bamboo.SentEmailViewerPlug.html

4 Likes

Thank you. This is the solution.

Thank you also for all the responses.

1 Like