2
votes

When i try to send emails through the mailgun library, i am getting the error: ** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1

I have captured the problem in a test:

test/mailers/mailer_test.ex

defmodule Myapp.MailerTest do
  use Myapp.ModelCase

  test "mailer test" do
    Myapp.Mailer.invitation_email("abc.com", "[email protected]")
  end
end

lib/mailer.ex

defmodule Myapp.Mailer do
  use Mailgun.Client, domain: "abc.mailgun.org",
                      key: "key-abc",
                      mode: :test,
                      mailgun_test_file_path: "mail.json"

  def invitation_email(host, to_email) do
    send_email to: to_email,
               from: "hi@#{host}",
               subject: "Invitation!",
               html: "<strong>Welcome</strong>"
  end
end

Test Run:

$ mix test test/mailers/mailer_test.ex                                                                                                     
Compiled lib/mailer.ex
Generated myapp app

  1) test mailer test (Myapp.MailerTest)
     test/mailers/mailer_test.ex:4
     ** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
     stacktrace:
       (elixir) lib/io.ex:333: IO.chardata_to_string(nil)
       (elixir) lib/file.ex:701: File.write/3
       lib/client.ex:22: Mailgun.Client.do_send_email/3
       test/mailers/mailer_test.ex:5

Finished in 0.2 seconds (0.1s on load, 0.01s on tests)
1 test, 1 failure

Randomized with seed 182269

Also, incidentally, the error goes away if i remove the mode parameter. But it does not send email as well.

1

1 Answers

2
votes
mailgun_test_file_path: "mail.json"

Should be:

test_file_path: "mail.json"

https://github.com/chrismccord/mailgun#test-mode