phoenix-framework - Elixir 邮件枪错误 : no function clause matching in IO. chardata_to_string/1

标签 phoenix-framework elixir

当我尝试通过 mailgun 库发送电子邮件时,出现错误:** (FunctionClauseError) IO.chardata_to_string/1 中没有匹配的函数子句

我在测试中发现了这个问题:

测试/mailers/mailer_test.ex

defmodule Myapp.MailerTest do
  use Myapp.ModelCase

  test "mailer test" do
    Myapp.Mailer.invitation_email("abc.com", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe989191be9c9f8cd09d9193" rel="noreferrer noopener nofollow">[email protected]</a>")
  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

测试运行:

$ 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

另外,顺便说一下,如果我删除 mode 参数,错误就会消失。但它也不发送电子邮件。

最佳答案

mailgun_test_file_path: "mail.json"

应该是:

test_file_path: "mail.json"

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

关于phoenix-framework - Elixir 邮件枪错误 : no function clause matching in IO. chardata_to_string/1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34047093/

相关文章:

elixir - 获取当前环境名称

elixir - 模式和模型有什么区别?

npm - Phoenix 框架 - 在子目录中执行 NPM 脚本

elixir - Phoenix 框架/Elixir 添加地理空间字段

elixir - Elixir 依赖库在哪里?

file-upload - 在 Elixir 中使用 Acr 将文件上传到本地

sockets - 我无法获得通过在不老长寿药, Phoenix 中的存在而加入 channel 的所有用户的列表

time - Elixir 中的 utc_offset 返回错误的时区偏移量

https - 向同一主机但不同主机 ip 发送 HTTPS get 请求

erlang - 如何修复在 Windows 上编译 bcrypt_elixir 或 argon_elixir 的错误?