email_address_validation.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. def valid!(check_mx: false)
  24. raise InvalidEmailAddressError if !valid?(check_mx:)
  25. end
  26. class InvalidEmailAddressError < StandardError
  27. def initialize
  28. super(__('The email address is invalid.'))
  29. end
  30. end
  31. end