Module: OnlyofficeIredmailHelper::MailGetters

Included in:
IredMailHelper
Defined in:
lib/onlyoffice_iredmail_helper/mail_getters.rb

Overview

Modules to get mail by different parameters

Instance Method Summary collapse

Instance Method Details

#email_by_date_and_title(date: Date.today, subject: nil, timeout: 300, move_out: true, range: 1) ⇒ Hash, False

Search email by specific date and message title

Parameters:

  • date (Date) (defaults to: Date.today)

    date to search

  • subject (String) (defaults to: nil)

    check if message is start_with this string

  • timeout (Integer) (defaults to: 300)

    How much time to wait in seconds

  • range (Integer) (defaults to: 1)

    range in days to extend specified date

Returns:

  • (Hash, False)

    mail data and false is none found



12
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
# File 'lib/onlyoffice_iredmail_helper/mail_getters.rb', line 12

def email_by_date_and_title(date: Date.today,
                            subject: nil,
                            timeout: 300,
                            move_out: true,
                            range: 1)
  start_date = (date - range).strftime('%d-%b-%Y')
  end_date = (date + range).strftime('%d-%b-%Y')

  
  @imap.select('INBOX')
  start_time = Time.now
  while (Time.now - start_time) < timeout
    @imap.search(['SINCE', start_date, 'BEFORE', end_date]).each do |message_id|
      mail_data = get_mail_data(message_id)
      next unless mail_data[:subject].start_with?(subject)

      if move_out
        move_out_message(message_id)
      else
        mark_message_as_seen(message_id)
      end
      return mail_data
    end
  end
  false
end

#get_html_body_email_by_subject(options = {}, times = 300) ⇒ String

Get email html body by subject

Parameters:

  • options (Hash) (defaults to: {})

    options of get

  • times (Integer) (defaults to: 300)

    count to check

Returns:

  • (String)

    html body



54
55
56
57
58
59
# File 'lib/onlyoffice_iredmail_helper/mail_getters.rb', line 54

def get_html_body_email_by_subject(options = {}, times = 300)
  mail = mail_by_subject(options, times)
  return nil unless mail

  mail[:html_body]
end

#get_text_body_email_by_subject(options = {}, times = 300) ⇒ String

Get email text body by subject

Parameters:

  • options (Hash) (defaults to: {})

    options of get

  • times (Integer) (defaults to: 300)

    count to check

Returns:

  • (String)

    text body



43
44
45
46
47
48
# File 'lib/onlyoffice_iredmail_helper/mail_getters.rb', line 43

def get_text_body_email_by_subject(options = {}, times = 300)
  mail = mail_by_subject(options, times)
  return nil unless mail

  mail[:body]
end