trabajo-afectivo/lib/email_helper/verify.rb

116 lines
2.5 KiB
Ruby
Raw Permalink Normal View History

# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
class EmailHelper
class Verify
=begin
get result of inbound probe
result = EmailHelper::Verify.email(
inbound: {
adapter: 'imap',
options: {
host: 'imap.gmail.com',
port: 993,
ssl: true,
user: 'some@example.com',
password: 'password',
},
},
outbound: {
adapter: 'smtp',
options: {
host: 'smtp.gmail.com',
port: 25,
ssl: true,
user: 'some@example.com',
password: 'password',
},
},
2015-08-28 00:53:14 +00:00
sender: 'sender_and_recipient_of_verify_email@example.com',
)
returns on success
{
result: 'ok'
}
returns on fail
{
result: 'invalid',
message: 'Verification Email not found in mailbox.',
subject: subject,
}
or
{
result: 'invalid',
message: 'Authentication failed!.',
subject: subject,
}
=end
def self.email(params)
# send verify email
subject = params[:subject].presence || "##{SecureRandom.hex(10)}"
2015-08-28 00:53:14 +00:00
result = EmailHelper::Probe.outbound(params[:outbound], params[:sender], subject)
if result[:result] != 'ok'
result[:source] = 'outbound'
return result
end
# validate adapter
adapter = params[:inbound][:adapter].downcase
2015-08-31 23:12:51 +00:00
if !EmailHelper.available_driver[:inbound][adapter.to_sym]
return {
result: 'failed',
message: "Unknown adapter '#{adapter}'",
}
end
# looking for verify email
9.times do
sleep 5
# fetch mailbox
fetch_result = nil
begin
driver_class = "Channel::Driver::#{adapter.to_classname}".constantize
driver_instance = driver_class.new
fetch_result = driver_instance.fetch(params[:inbound][:options], self, 'verify', subject)
rescue => e
2015-08-23 21:25:31 +00:00
result = {
result: 'invalid',
source: 'inbound',
message: e.to_s,
message_human: EmailHelper::Probe.translation(e.message),
invalid_field: EmailHelper::Probe.invalid_field(e.message),
subject: subject,
}
2015-08-23 21:06:21 +00:00
return result
end
next if !fetch_result
next if fetch_result[:result] != 'ok'
return fetch_result
end
{
result: 'invalid',
message: 'Verification Email not found in mailbox.',
subject: subject,
}
end
end
end