defmodule RecycledCloud.SupportRequest do use Ecto.Schema import Ecto.Changeset import Bamboo.Email alias RecycledCloud.SupportRequest alias RecycledCloud.Mailer embedded_schema do field :name, :string field :email, :string field :message, :string timestamps() end def changeset(key, attrs) do key |> cast(attrs, [:name, :email, :message]) |> validate_required([:name, :email, :message]) 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