verify.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class EmailHelper
  3. class Verify
  4. =begin
  5. get result of inbound probe
  6. result = EmailHelper::Verify.email(
  7. inbound: {
  8. adapter: 'imap',
  9. options: {
  10. host: 'imap.gmail.com',
  11. port: 993,
  12. ssl: true,
  13. user: 'some@example.com',
  14. password: 'password',
  15. },
  16. },
  17. outbound: {
  18. adapter: 'smtp',
  19. options: {
  20. host: 'smtp.gmail.com',
  21. port: 25,
  22. ssl: true,
  23. user: 'some@example.com',
  24. password: 'password',
  25. },
  26. },
  27. sender: 'sender_and_recipient_of_verify_email@example.com',
  28. )
  29. returns on success
  30. {
  31. result: 'ok'
  32. }
  33. returns on fail
  34. {
  35. result: 'invalid',
  36. message: 'Verification Email not found in mailbox.',
  37. subject: subject,
  38. }
  39. or
  40. {
  41. result: 'invalid',
  42. message: 'Authentication failed!.',
  43. subject: subject,
  44. }
  45. =end
  46. def self.email(params)
  47. # send verify email
  48. subject = params[:subject].presence || "##{SecureRandom.hex(10)}"
  49. result = EmailHelper::Probe.outbound(params[:outbound], params[:sender], subject)
  50. if result[:result] != 'ok'
  51. result[:source] = 'outbound'
  52. return result
  53. end
  54. # validate adapter
  55. adapter = params[:inbound][:adapter].downcase
  56. if !EmailHelper.available_driver[:inbound][adapter.to_sym]
  57. return {
  58. result: 'failed',
  59. message: "Unknown adapter '#{adapter}'",
  60. }
  61. end
  62. # looking for verify email
  63. 9.times do
  64. sleep 5
  65. # fetch mailbox
  66. fetch_result = nil
  67. begin
  68. driver_class = "Channel::Driver::#{adapter.to_classname}".constantize
  69. driver_instance = driver_class.new
  70. fetch_result = driver_instance.fetch(params[:inbound][:options], self, 'verify', subject)
  71. rescue => e
  72. result = {
  73. result: 'invalid',
  74. source: 'inbound',
  75. message: e.to_s,
  76. message_human: EmailHelper::Probe.translation(e.message),
  77. invalid_field: EmailHelper::Probe.invalid_field(e.message),
  78. subject: subject,
  79. }
  80. return result
  81. end
  82. next if !fetch_result
  83. next if fetch_result[:result] != 'ok'
  84. return fetch_result
  85. end
  86. {
  87. result: 'invalid',
  88. message: __('Verification Email not found in mailbox.'),
  89. subject: subject,
  90. }
  91. end
  92. end
  93. end