email_address_validation.rb 830 B

123456789101112131415161718192021222324252627282930
  1. # Copyright (C) 2012-2024 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. # @param mx [Boolean] check only syntax or MX as well
  16. #
  17. # @return [true] if email address has valid format
  18. # @return [false] if email address has no valid format
  19. def valid?(check_mx: false)
  20. host_validation_type = check_mx ? :mx : :syntax
  21. EmailAddressValidator.valid? email_address, host_validation: host_validation_type
  22. end
  23. end