2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2020-02-05 12:50:49 +00:00
|
|
|
class EmailHelper
|
2015-08-23 20:21:04 +00:00
|
|
|
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',
|
2015-08-23 20:21:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2021-09-20 10:47:05 +00:00
|
|
|
subject = params[:subject].presence || "##{SecureRandom.hex(10)}"
|
2015-08-28 00:53:14 +00:00
|
|
|
result = EmailHelper::Probe.outbound(params[:outbound], params[:sender], subject)
|
2015-08-29 11:46:48 +00:00
|
|
|
if result[:result] != 'ok'
|
|
|
|
result[:source] = 'outbound'
|
|
|
|
return result
|
|
|
|
end
|
2015-08-23 20:21:04 +00:00
|
|
|
|
2015-08-30 11:58:05 +00:00
|
|
|
# validate adapter
|
|
|
|
adapter = params[:inbound][:adapter].downcase
|
2015-08-31 23:12:51 +00:00
|
|
|
if !EmailHelper.available_driver[:inbound][adapter.to_sym]
|
2015-08-30 11:58:05 +00:00
|
|
|
return {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'failed',
|
2015-08-30 11:58:05 +00:00
|
|
|
message: "Unknown adapter '#{adapter}'",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-08-23 20:21:04 +00:00
|
|
|
# looking for verify email
|
2017-10-01 12:25:52 +00:00
|
|
|
9.times do
|
2015-08-29 11:46:48 +00:00
|
|
|
sleep 5
|
2015-08-23 20:21:04 +00:00
|
|
|
|
|
|
|
# fetch mailbox
|
2015-09-06 07:50:51 +00:00
|
|
|
fetch_result = nil
|
2015-08-23 20:21:04 +00:00
|
|
|
|
|
|
|
begin
|
2019-01-06 18:41:29 +00:00
|
|
|
driver_class = "Channel::Driver::#{adapter.to_classname}".constantize
|
2015-08-30 11:58:05 +00:00
|
|
|
driver_instance = driver_class.new
|
2015-09-06 07:50:51 +00:00
|
|
|
fetch_result = driver_instance.fetch(params[:inbound][:options], self, 'verify', subject)
|
2015-08-23 20:21:04 +00:00
|
|
|
rescue => e
|
2015-08-23 21:25:31 +00:00
|
|
|
result = {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
|
|
|
source: 'inbound',
|
|
|
|
message: e.to_s,
|
2015-08-29 11:46:48 +00:00
|
|
|
message_human: EmailHelper::Probe.translation(e.message),
|
|
|
|
invalid_field: EmailHelper::Probe.invalid_field(e.message),
|
2018-12-19 17:31:51 +00:00
|
|
|
subject: subject,
|
2015-08-23 20:21:04 +00:00
|
|
|
}
|
2015-08-23 21:06:21 +00:00
|
|
|
return result
|
2015-08-23 20:21:04 +00:00
|
|
|
end
|
|
|
|
|
2015-09-06 07:50:51 +00:00
|
|
|
next if !fetch_result
|
|
|
|
next if fetch_result[:result] != 'ok'
|
2015-08-23 20:21:04 +00:00
|
|
|
|
2015-09-06 07:50:51 +00:00
|
|
|
return fetch_result
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-23 20:21:04 +00:00
|
|
|
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2015-08-23 20:21:04 +00:00
|
|
|
message: 'Verification Email not found in mailbox.',
|
|
|
|
subject: subject,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|