This straightforward Ruby class will connect to Mailgun's Email Validation Service and tell you if the provided email is valid or not.
Configure your Mailgun public key:
EmailValidation.configure do |config|
config.mailgun_public_key = 'pubkey-5ogiflzbnjrljiky49qxsiozqef5jxp7'
end
EmailValidation::Validate.new("[email protected]").valid? # => false
or do it in two steps if that's more your style:
validation = EmailValidation::Validate.new("[email protected]")
validation.valid? # => false
I suggest you create a background job to check email validity. You don't really want to depend on Mailgun being online in order to register a new user. A possible solution using a boolean field on User is this:
require 'email_validation/validate'
class EmailValidationJob < ApplicationJob
queue_as :default
def perform(user)
user.update email_valid: email_valid(user.email)
end
private
def email_valid(email)
EmailValidation::Validate.new(email).valid?
end
end
You can then ask the user to change their email.
Add this line to your application's Gemfile:
gem 'email_validation', github: 'sign2pay/email_validation'
Currently this gem hasn't been released to Rubygems. We would need a new name I think. If anyone is interested in me doing that, open an issue.
To install this gem:
$ bundle
rake test
Open an issue or submit a pull request. We'll work it out!
Copyright Sign2Pay NV. Licensed using the MIT license.