This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Validates that MX record exists for domain to prevent typos and also block by MX servers | |
# Use by adding to app/models/user.rb | |
# validates_with EmailMXValidator, if: :email_changed? | |
# frozen_string_literal: true | |
require 'resolv' | |
class EmailMXValidator < ActiveModel::Validator | |
def validate(user) | |
domain = user.email.split('@', 2).last | |
mxs = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s } |