我有一个发送email
和message
字段的简单联系表单。我使用的是Mailgun。
我可以从控制台完美地发送电子邮件,但是当我尝试从UI中的表单发送时,我收到以下错误:
protocol String.Chars not implemented for %Iotc.Contact.Email{__meta__:
#Ecto.Schema.Metadata<:loaded, "contact_emails">, email:
"cooper.sf@gmail.com", id: 25, inserted_at: ~N[2017-06-15 10:34:26.229957],
message: "I'd like a cake please.", updated_at: ~N[2017-06-15 10:34:26.229977]}
email_controller.ex
def create(conn, %{"email" => email_params,
"email" => %{"email" => email,
"message" => message}}) do
changeset = Contact.Email.changeset(%Iotc.Contact.Email{}, email_params)
case Contact.create_email(email_params) do
{:ok, email} ->
Iotc.Mailer.send_email(email, message) #<this is the issue.
conn
|> put_flash(:info, "Email sent")
|> redirect(to: email_path(conn, :index))
{:error, changeset} ->
conn
|> put_flash(:error, "Something went wrong")
|> render("index.html", changeset: changeset)
end
我尝试过改变
Iotc.Mailer.send_email(email, message)
到
Iotc.Mailer.send_email(email_params)
但我接着
key :to not found in: %{"email" => "cooper.sf@gmail.com",
"message" => "I'd like a cake please.", "name" => "Simon"}
答案 0 :(得分:1)
模式{:ok, email}
使用email
结构隐藏外部%Iotc.Contact.Email{}
字符串值,而send_email
期望第一个参数只是电子邮件地址字符串值。您可以通过email.email
:
case Contact.create_email(email_params) do
{:ok, email} ->
Iotc.Mailer.send_email(email.email, message)
...
...
end
或者您可以更改模式以忽略返回的结构,因为您还没有使用它:
case Contact.create_email(email_params) do
{:ok, _} ->
Iotc.Mailer.send_email(email, message)
...
...
end