set_email_notification_configuration.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::System::SetEmailNotificationConfiguration < Service::Base
  3. # Setup Email Notification channel configuration
  4. #
  5. # @param [String] adapter sendmail or smtp
  6. # @param [Hash] new_configuration email server configuration, empty unless adapter is smtp
  7. # @option new_configuration [String] :host SMTP server address
  8. # @option new_configuration [String] :port SMTP server port
  9. # @option new_configuration [Boolean] :ssl Wether SMTP ses TLS/SSL
  10. # @option new_configuration [String] :user login of SMTP server
  11. # @option new_configuration [String] :password of SMTP server
  12. # @option new_configuration [Boolean] :ssl_verify Wether SSL verification is performed
  13. def initialize(adapter:, new_configuration:)
  14. super()
  15. @adapter = adapter
  16. @new_configuration = new_configuration
  17. end
  18. def execute
  19. # There're two instances of Email::Notification for historical easons
  20. # One for SMTP and one for Sendmail.
  21. # However, this feature is not used anywhere.
  22. # At some point it may be good to clean this up to simply use a single instance
  23. # and set adapter as needed.
  24. ActiveRecord::Base.transaction do
  25. Channel
  26. .where(area: 'Email::Notification')
  27. .each { update_single_channel(_1) }
  28. end
  29. true
  30. end
  31. private
  32. def update_single_channel(channel)
  33. is_matching_adapter = @adapter.casecmp? channel.options.dig(:outbound, :adapter)
  34. channel.active = is_matching_adapter
  35. if is_matching_adapter
  36. channel.options = {
  37. outbound: {
  38. adapter: @adapter,
  39. options: @new_configuration,
  40. },
  41. }
  42. channel.status_out = 'ok'
  43. channel.last_log_out = nil
  44. end
  45. channel.save!
  46. end
  47. end