How to Validate Email Address in Ruby on Rails

Check if an email address is correct and really exists using Ruby on Rails

Email Validation in a Nutshell

An Email Address can look right but still be wrong and bounce. We’re providing you our api to use Ruby on Rails and validate all those emails coming into your various workspaces.

Email Validation Using an API Key

With an API key you wont be limited by how many addresses you can check at one time, or how fast you can check them. In the Ruby example changes to add the API key shown below. You can of course also just use our bulk email validation upload tool if you aren’t interested in using the api and RoR.

				
					require 'net/http'
require 'json'

api_key = '' # Generated in your User Profile; it shows at the top in a green bar once
team_slug = '' # When you sign up, you have a team; it's in the URL; then use that
email_address = 'test@test.com' # The test email

url = "https://app.mailvalidation.io/a/#{team_slug}/validate/api/validate/"
uri = URI(url)

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Post.new(uri.path,
  { 'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'Authorization' => "Api-Key #{api_key}" })

request.body = { email: email_address }.to_json

response = http.request(request)

if response.code.to_i == 200
  result = JSON.parse(response.body)
  valid = result['is_valid']
  if valid
    puts 'Valid'
  else
    puts 'Invalid'
  end
else
  puts "An error occurred: #{response.code} #{response.message}"
end
				
			

Support

Are you having trouble, have some questions. How can we help?