1 # frozen_string_literal: true
3 # Validates that an email address is both RFC-compliant, using the
4 # validates_email_format_of gem, and will also be accepted by exim relays.
6 # Exim's default configuration rejects recipients on remote domains whose
9 # local_parts = ^[./|] : ^.*[@%!] : ^.*/\\.\\./
11 # Many such addresses are valid using RFC 5322, so otherwise the gem would accept them.
13 class DeliverableEmailValidator < ActiveModel::EachValidator
14 INVALID_LOCAL_PART_CHARS = "@%!"
15 INVALID_LOCAL_PART = %r{\A[./|]|/\.\./}
17 def validate_each(record, attribute, value)
18 errors = ValidatesEmailFormatOf.validate_email_format(value, options) || exim_errors(value)
20 errors.each { |error| record.errors.add(attribute, error) }
25 def exim_errors(value)
26 local_part = value.rpartition("@").first
29 errors << I18n.t("validations.email_characters", :characters => INVALID_LOCAL_PART_CHARS) if /[#{INVALID_LOCAL_PART_CHARS}]/o.match?(local_part)
30 errors << I18n.t("validations.invalid_characters") if INVALID_LOCAL_PART.match?(local_part)