13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/email/verification/gmail.rb', line 13
def normal_retrieve_verification_code(email:, password:, mark_as_read: true, count: :all, mailboxes: %w(Inbox), settings: {})
emails = []
result = nil
begin
args = settings_provided?(settings) ? {from: settings[:address]} : {}
::Gmail.connect(email, password) do |gmail|
gmail.inbox.emails(args).each do |email|
log("Email - from: #{email.from.first.name}, subject: #{email.subject}")
if settings_provided?(settings)
matching_name = settings[:from].to_s.empty? || (!settings[:from].to_s.empty? && email.from.first.name == settings[:from])
matching_subject = settings[:subject].nil? || (!settings[:subject].nil? && !(email.subject =~ settings[:subject]).nil?)
emails << email_body(email) if matching_name && matching_subject
else
emails << email_body(email)
end
email.read! if mark_as_read
end
end
rescue Net::IMAP::BadResponseError => e
raise ::Email::Verification::Errors::InvalidCredentialsError.new("You need to enable logins for less secure apps in Gmail for #{email}!")
end
if settings_provided?(settings)
message = emails.last&.to_s
result = message&.match(settings[:regex])&.[](:match)
else
result = emails
end
return result
end
|