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
|
2015-08-28 00:53:14 +00:00
|
|
|
if !params[:subject] || params[:subject].empty?
|
2015-08-23 20:21:04 +00:00
|
|
|
subject = '#' + rand(99_999_999_999).to_s
|
|
|
|
else
|
|
|
|
subject = 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
|
|
|
|
|
|
|
# looking for verify email
|
2015-08-29 11:46:48 +00:00
|
|
|
(1..10).each {
|
|
|
|
sleep 5
|
2015-08-23 20:21:04 +00:00
|
|
|
|
|
|
|
# fetch mailbox
|
|
|
|
found = nil
|
|
|
|
|
|
|
|
begin
|
|
|
|
if params[:inbound][:adapter] =~ /^imap$/i
|
2015-08-28 00:53:14 +00:00
|
|
|
found = Channel::Driver::Imap.new.fetch(params[:inbound][:options], self, 'verify', subject)
|
2015-08-23 20:21:04 +00:00
|
|
|
else
|
2015-08-28 00:53:14 +00:00
|
|
|
found = Channel::Driver::Pop3.new.fetch(params[:inbound][:options], self, 'verify', subject)
|
2015-08-23 20:21:04 +00:00
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
next if !found
|
|
|
|
next if found != 'verify ok'
|
|
|
|
|
|
|
|
return {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
result: 'invalid',
|
|
|
|
message: 'Verification Email not found in mailbox.',
|
|
|
|
subject: subject,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|