email_address_validation.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. # Validation for email addresses
  3. class EmailAddressValidation
  4. attr_reader :email_address
  5. # @param [String] email_address Email address to be validated
  6. def initialize(email_address)
  7. @email_address = email_address
  8. end
  9. def to_s
  10. email_address
  11. end
  12. # Checks if the email address has a valid format.
  13. # Reports email addresses without dot in domain as valid (zammad@localhost).
  14. #
  15. # @return [true] if email address has valid format
  16. # @return [false] if email address has no valid format
  17. def valid_format?
  18. # NOTE: Don't use ValidEmail2::Address.valid? here because it requires the
  19. # email address to have a dot in its domain.
  20. @valid_format ||= email_address.match?(URI::MailTo::EMAIL_REGEXP)
  21. end
  22. # Checks if the domain of the email address has a valid MX record.
  23. #
  24. # @return [true] if email address domain has an MX record
  25. # @return [false] if email address domain has no MX record
  26. def valid_mx?
  27. return @valid_mx if @valid_mx
  28. validated_email_address = ValidEmail2::Address.new(email_address)
  29. @valid_mx = validated_email_address&.valid_mx?
  30. end
  31. end