<commit_before>defmodule SendGrid.Mailer do
  @moduledoc """
  Module to send transactional email.

  To send emails in sandbox mode, ensure the config key is set:
  ```elixir
      config :sendgrid,
        api_key: "SENDGRID_API_KEY",
        sandbox_enable: true
  ```
  """

  alias SendGrid.Email

  @mail_url "/v3/mail/send"
  @sandbox_mode Application.get_env(:sendgrid, :sandbox_enable) || false


  @doc """
  Sends the built email.

      email =
        Email.build()
        |> Email.put_to("<EMAIL>")
        |> Email.put_from("<EMAIL>")
        |> Email.put_subject("Hello from Elixir")
        |> Email.put_text("Sent with Elixir")

      :ok = Mailer.send(email)

  """
  @spec send(SendGrid.Email.t) :: :ok | { :error, list(String.t) } | { :error, String.t }
  def send(%Email{} = email) do
    payload =
      email
      |> format_email_for_sending

    case SendGrid.post(@mail_url, payload, [{ "Content-Type", "application/json" }]) do
      { :ok, %{ status_code: 202 } } -> :ok
      { :ok, %{ body: body } } -> { :error, body["errors"] }
      _ -> { :error, "Unable to communicate with SendGrid API." }
    end
  end

  defp format_email_for_sending(%Email{} = email) do
    personalizations =
      email
      |> Map.take([:to, :cc, :bcc, :substitutions])
      |> Stream.into([])
      |> Stream.filter(fn { _key, v } -> v != nil && v != [] end)
      |> Enum.into(%{})

    %{
      personalizations: [personalizations],
      from: email.from,
      subject: email.subject,
      content: email.content,
      reply_to: email.reply_to,
      send_at: email.send_at,
      template_id: email.template_id,
      mail_settings: %{ sandbox_mode: %{ enable: @sandbox_mode } }
    }
  end
end<commit_msg>[sandbox] Fixing parse of sandbox response with status_code 200<commit_after>defmodule SendGrid.Mailer do
  @moduledoc """
  Module to send transactional email.

  To send emails in sandbox mode, ensure the config key is set:
  ```elixir
      config :sendgrid,
        api_key: "SENDGRID_API_KEY",
        sandbox_enable: true
  ```
  """

  alias SendGrid.Email

  @mail_url "/v3/mail/send"

  @doc """
  Sends the built email.

      email =
        Email.build()
        |> Email.put_to("<EMAIL>")
        |> Email.put_from("<EMAIL>")
        |> Email.put_subject("Hello from Elixir")
        |> Email.put_text("Sent with Elixir")

      :ok = Mailer.send(email)

  """
  @spec send(SendGrid.Email.t) :: :ok | { :error, list(String.t) } | { :error, String.t }
  def send(%Email{} = email) do
    payload =
      email
      |> format_email_for_sending

    case SendGrid.post(@mail_url, payload, [{ "Content-Type", "application/json" }]) do
      { :ok, %{ status_code: 202 } } -> :ok
      { :ok, %{ status_code: 200 } } -> :ok
      { :ok, %{ body: body } } -> { :error, body["errors"] }
      _ -> { :error, "Unable to communicate with SendGrid API." }
    end
  end

  defp format_email_for_sending(%Email{} = email) do
    personalizations =
      email
      |> Map.take([:to, :cc, :bcc, :substitutions])
      |> Stream.into([])
      |> Stream.filter(fn { _key, v } -> v != nil && v != [] end)
      |> Enum.into(%{})

    %{
      personalizations: [personalizations],
      from: email.from,
      subject: email.subject,
      content: email.content,
      reply_to: email.reply_to,
      send_at: email.send_at,
      template_id: email.template_id,
      mail_settings: %{ sandbox_mode: %{ enable: sandbox_mode } }
    }
  end

  defp sandbox_mode, do: Application.get_env(:sendgrid, :sandbox_enable) || false
end
{'fraction_non_alphanumeric': 0.1537380550871276, 'fraction_numerical': 0.0039347948285553686, 'mean_word_length': 2.6427840327533265, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '476434', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1194, 'n_words': 273}