meta/lib/meta/support_request.ex
2021-04-12 15:44:12 +02:00

50 lines
1.2 KiB
Elixir

defmodule Meta.SupportRequest do
use Ecto.Schema
import Ecto.Changeset
import Bamboo.Email
alias Meta.SupportRequest
alias Meta.Mailer
alias Meta.Captcha
embedded_schema do
field :name, :string
field :email, :string
field :message, :string
field :captcha, :integer
timestamps()
end
def changeset(key, attrs) do
expected_captcha_result = attrs |> Map.get("expected_captcha")
key
|> cast(attrs, [:name, :email, :message, :captcha])
|> validate_required([:name, :email, :message, :captcha])
|> Captcha.validate(expected_captcha_result)
end
def send(%SupportRequest{} = request) do
admin_email = Application.get_env(:meta, :admin_email)
host = Application.get_env(:meta, MetaWeb.Endpoint)
|> Keyword.get(:url, [])
|> Keyword.get(:host, "localhost")
body = """
A new request has been received on #{host}:
From: #{request.name} // #{request.email}
~~~
#{request.message}
"""
Mailer.template()
|> to(request.email)
|> bcc(admin_email)
|> put_header("Reply-To", request.email)
|> subject("Recycled Cloud Support Request")
|> text_body(body)
|> Mailer.deliver_now()
end
end