当我尝试通过 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"
关于phoenix-framework - Elixir 邮件枪错误 : no function clause matching in IO. chardata_to_string/1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34047093/