chrismccord / mailgun

Elixir Mailgun Client
MIT License
194 stars 95 forks source link

Config errors when a dependency depends on Mailgun #24

Open Zensavona opened 9 years ago

Zensavona commented 9 years ago

Hi, I'm having some issues with the config being set when using mailgun from within a library I'm building, it's very possible I'm at fault here but I'm new to Elixir so maybe you can point me in the right direction...

I'm getting this error:

[error] #PID<0.454.0> running HrTest.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /user/new
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
        (elixir) lib/io.ex:330: IO.chardata_to_string(nil)
        (elixir) lib/path.ex:467: Path.join/2
        (elixir) lib/path.ex:449: Path.join/1
        (mailgun) lib/client.ex:44: Mailgun.Client.send_without_attachments/2
        (hr) lib/hr/form_controller.ex:258: Hr.FormController.create_signup/2
        (hr) lib/hr/form_controller.ex:257: Hr.FormController.action/2
        (hr) lib/hr/form_controller.ex:257: Hr.FormController.phoenix_controller_pipeline/2
        (hr_test) lib/phoenix/router.ex:255: HrTest.Router.dispatch/2
        (hr_test) web/router.ex:1: HrTest.Router.do_call/2
        (hr_test) lib/hr_test/endpoint.ex:1: HrTest.Endpoint.phoenix_pipeline/1
        (hr_test) lib/plug/debugger.ex:90: HrTest.Endpoint."call (overridable 3)"/2
        (hr_test) lib/phoenix/endpoint/render_errors.ex:34: HrTest.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

So, I've got a dependency to my main application which has a mailer like this:

defmodule Hr.MailHelper do
  use Mailgun.Client, domain: Application.get_env(:hr, :mailgun_domain),
                     key: Application.get_env(:hr, :mailgun_key),
                     mode: Application.get_env(:hr, :mailgun_mode),
                     test_file_path: Application.get_env(:hr, :mailgun_test_file_path)
  @dir "web/templates/hr_email/"

  def send_confirmation_email(user, link) do
    send_email to: user.unconfirmed_email,
               from: Application.get_env(:hr, :register_from_email),
               subject: Application.get_env(:hr, :confirmation_email_subject),
               html: EEx.eval_file(@dir <> "signup_confirmation.eex", [user: user, link: link])
  end

  def send_reset_email(user, link) do
    send_email to: user.email,
               from: Application.get_env(:hr, :password_recovery_from_email),
               subject: Application.get_env(:hr, :recovery_email_subject),
               html: EEx.eval_file(@dir <> "password_reset.eex", [user: user, link: link])
  end
end

and the main application (which depends on the library which includes that mailer) has these configs set as:

config :hr, register_from_email: "Registration <welcome@example.com>",
            password_recovery_from_email: "Password Recovery <no-reply@example.com>",
            confirmation_email_subject: "Confirm Your Account",
            recovery_email_subject: "Reset Your Password",
            mailgun_domain: "https://api.mailgun.net/v3/mydomain.com",
            mailgun_key: "key-##############",
            mailgun_mode: :test,
            mailgun_test_file_path: "/tmp/mail.json"

I've done a bit of digging and if I pop a IO.inspect conf and IO.inspect email in at Line 44 of mailgun/lib/client.ex, it is getting the email, but not the conf, so my application config is working... The other interesting thing is that if I make a change to the mailer in the library, it recompiles(?) and then everything works. If I include the library into a fresh mix.phoenix project, it breaks again (in the new application, but not the old one). I am guessing this is something to do with compilation of macros?

FWIW I've tried using {:hr, path: "../path"} and {:hr, git: "some-git-url"} in the hope that if it's fetched remotely it will be recompiled and achieve the same effect as making a change to the source, but they behave exactly the same.

Do you have any idea what could be causing this / Is there a way around it?

Thanks!

Zensavona commented 8 years ago

not sure if this issue is even mailgun related, but here is the relevant mailing list discussion :)

https://groups.google.com/forum/#!topic/elixir-lang-talk/OzHSkE328Js

duksis commented 8 years ago

Having the same issue on heroku.

backspace commented 8 years ago

Hey, @duksis, I was able to get it working on Heroku by creating elixir_buildpack.config with this line:

config_vars_to_export=(DATABASE_URL MAILGUN_KEY)
duksis commented 8 years ago

@backspace thanks - that works for me as well