verify.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class EmailHelper
  3. class Verify
  4. #
  5. # Validate an email channel configuration first sending and then fetching that same mail.
  6. #
  7. def self.email(params)
  8. # send verify email
  9. subject = params[:subject].presence || "##{SecureRandom.hex(10)}"
  10. result = EmailHelper::Probe.outbound(params[:outbound], params[:sender], subject)
  11. if result[:result] != 'ok'
  12. result[:source] = 'outbound'
  13. return result
  14. end
  15. # validate adapter
  16. adapter = params[:inbound][:adapter].downcase
  17. if !EmailHelper.available_driver[:inbound][adapter.to_sym]
  18. return {
  19. result: 'failed',
  20. message: "Unknown adapter '#{adapter}'",
  21. }
  22. end
  23. # looking for verify email
  24. 9.times do
  25. sleep 5
  26. # fetch mailbox
  27. fetch_result = nil
  28. begin
  29. driver_class = "Channel::Driver::#{adapter.to_classname}".constantize
  30. driver_instance = driver_class.new
  31. fetch_result = driver_instance.fetch(params[:inbound][:options], self, 'verify', subject)
  32. rescue => e
  33. result = {
  34. result: 'invalid',
  35. source: 'inbound',
  36. message: e.to_s,
  37. message_human: EmailHelper::Probe.translation(e.message),
  38. invalid_field: EmailHelper::Probe.invalid_field(e.message),
  39. subject: subject,
  40. }
  41. return result
  42. end
  43. next if !fetch_result
  44. next if fetch_result[:result] != 'ok'
  45. return fetch_result
  46. end
  47. {
  48. result: 'invalid',
  49. message: __('Verification Email not found in mailbox.'),
  50. subject: subject,
  51. }
  52. end
  53. end
  54. end