Skip to content

Instantly share code, notes, and snippets.

@hmorneau
Last active March 21, 2024 14:47
Show Gist options
  • Save hmorneau/4be3fa98b92c2d4dd1330752a416a7da to your computer and use it in GitHub Desktop.
Save hmorneau/4be3fa98b92c2d4dd1330752a416a7da to your computer and use it in GitHub Desktop.
api email
defmodule Studio.Accounts.EmailValidator do
@moduledoc """
Reoon Email Verifier API client. The score is the probability that the email is safe to send.
"""
@api_base_url "https://emailverifier.reoon.com/api/v1/verify"
@minimum_score 70
# Public function to check if an email is safe to send, with an optional mode parameter
def validate(email, mode \\ "power") do
api_key = Application.get_env(:studio, :email_validator_api_key)
case api_call(email, mode, api_key) do
{:ok, email_verification_result} ->
Map.get(email_verification_result, "overall_score", 0) > @minimum_score
{:error, _reason} ->
false
end
end
# Private function to call the email verification API, including the mode parameter
defp api_call(email, mode, api_key) do
url =
"#{@api_base_url}?email=#{URI.encode(email)}&key=#{URI.encode(api_key)}&mode=#{URI.encode(mode)}"
with {:ok, _status_code, _headers, client} <- :hackney.get(url, [], "", []),
{:ok, body} <- :hackney.body(client),
{:ok, decoded_body} <- Poison.decode(body) do
{:ok, decoded_body}
else
_error -> {:error, :failed_to_verify_email}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment