meta/lib/recycledcloud/support_request.ex

51 lines
1.3 KiB
Elixir

defmodule RecycledCloud.SupportRequest do
use Ecto.Schema
import Ecto.Changeset
import Bamboo.Email
alias RecycledCloud.SupportRequest
alias RecycledCloud.Mailer
alias RecycledCloud.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(:recycledcloud, :admin_email)
host = Application.get_env(:recycledcloud, RecycledCloudWeb.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