How to validate email addresses in Ruby

This tutorial will cover how to fully validate an email address using one of our APIs. Start by adding this to your Gemfile:

gem 'cloudmersive-validate-api-client', '~> 1.3.2'

Then call email_full_validation:

# load the gem
require 'cloudmersive-validate-api-client'
# setup authorization
CloudmersiveValidateApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
api_instance = CloudmersiveValidateApiClient::EmailApi.newemail = "email_example" # String | Email address to validate, e.g. \"\". The input is a string so be sure to enclose it in double-quotes.begin
#Fully validate an email address
result = api_instance.email_full_validation(email)
p result
rescue CloudmersiveValidateApiClient::ApiError => e
puts "Exception when calling EmailApi->email_full_validation: #{e}"

Easy. Let’s look at an example output using

"ValidAddress": true,
"MailServerUsedForValidation": "",
"Valid_Syntax": true,
"Valid_Domain": true,
"Valid_SMTP": true,
"IsCatchallDomain": false,
"Domain": "",
"IsFreeEmailProvider": false,
"IsDisposable": false

As you can see, our API not only checks the syntax of the address (which is all that most email validation does), but also queries the mail server to guarantee its accuracy.

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store