2015-08-23 20:21:04 +00:00
|
|
|
module 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',
|
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
|
2017-06-28 17:13:52 +00:00
|
|
|
subject = if params[:subject].blank?
|
2016-01-15 17:22:57 +00:00
|
|
|
'#' + rand(99_999_999_999).to_s
|
|
|
|
else
|
|
|
|
params[:subject]
|
|
|
|
end
|
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 {
|
|
|
|
result: 'failed',
|
|
|
|
message: "Unknown adapter '#{adapter}'",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-08-23 20:21:04 +00:00
|
|
|
# looking for verify email
|
2016-06-30 20:04:48 +00:00
|
|
|
9.times {
|
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
|
2015-08-30 11:58:05 +00:00
|
|
|
require "channel/driver/#{adapter.to_filename}"
|
|
|
|
|
|
|
|
driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
|
|
|
|
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 = {
|
2015-08-23 20:21:04 +00:00
|
|
|
result: 'invalid',
|
2015-08-29 11:46:48 +00:00
|
|
|
source: 'inbound',
|
2015-08-23 20:21:04 +00:00
|
|
|
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),
|
2015-08-23 20:21:04 +00:00
|
|
|
subject: subject,
|
|
|
|
}
|
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
|
2015-08-23 20:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
result: 'invalid',
|
|
|
|
message: 'Verification Email not found in mailbox.',
|
|
|
|
subject: subject,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|