class OnlyofficeIredmailHelper::IredMailHelper

Class for working with mail

Attributes

username[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/onlyoffice_iredmail_helper.rb, line 24
def initialize(options = {})
  read_defaults
  @domainname = options[:domainname] || @default_domain
  @username = options[:username] || @default_user
  @password = options[:password] || @default_password
  @subject = options[:subject] || @default_subject
  @body = options[:body]
  @mailbox_for_archive = 'checked'
end

Public Instance Methods

check_email_by_subject(options = {}, times = 300, move_out = false) click to toggle source

Check if message exists by params @param options [Hash] options of get @param times [Integer] count to check @return [True, False] result of check

# File lib/onlyoffice_iredmail_helper.rb, line 128
def check_email_by_subject(options = {}, times = 300, move_out = false)
  login
  @imap.select('INBOX')
  start_time = Time.now
  while Time.now - start_time < times
    get_emails_search_or_new(options).each do |message_id|
      string_found = get_mail_data(message_id, options[:search])[:subject].to_s.upcase.gsub(/\s+/, ' ')
      string_to_be_found = options[:subject].to_s.upcase.gsub(/\s+/, ' ')
      next unless string_found.include? string_to_be_found

      if move_out
        move_out_message(message_id)
      else
        mark_message_as_seen(message_id)
      end
      return true
    end
  end
  false
end
create_msg(msg_data = {}) click to toggle source

Form a email string @param msg_data [Hash] params @return [String] formed mail message

# File lib/onlyoffice_iredmail_helper.rb, line 53
    def create_msg(msg_data = {})
      <<~END_OF_MESSAGE
        From: #{@username}
        To: #{msg_data[:mailto]}
        Subject: #{msg_data[:subject]}
        Date: #{Time.now.rfc2822}
        Message-Id: "#{Digest::SHA2.hexdigest(Time.now.to_i.to_s)}@#{@username.split('@').last}"
        
        #{msg_data[:body]}
      END_OF_MESSAGE
    end
delete_all_messages() click to toggle source

Delete all messages in inbox @return [nil]

# File lib/onlyoffice_iredmail_helper.rb, line 79
def delete_all_messages
  login
  @imap.select('INBOX')
  @imap.store(@imap.search(['ALL']), '+FLAGS', [:Deleted]) unless @imap.search(['ALL']).empty?
  OnlyofficeLoggerHelper.log('Delete all messages')
  close
end
delete_email_by_subject(subject) click to toggle source

Delete email by subject @param subject [String] email title @return [nil]

# File lib/onlyoffice_iredmail_helper.rb, line 90
def delete_email_by_subject(subject)
  login
  @imap.select('INBOX')
  id_emails = @imap.search(['SUBJECT', subject.dup.force_encoding('ascii-8bit')])
  @imap.store(id_emails, '+FLAGS', [:Deleted]) unless id_emails.empty?
  close
end
get_email_by_subject(options = {}, times = 300, move_out = false) click to toggle source

Get email by subject @param options [Hash] options of get @param times [Integer] count to check @param move_out [True, False] should message to move out @return [Hash] mail

# File lib/onlyoffice_iredmail_helper.rb, line 154
def get_email_by_subject(options = {}, times = 300, move_out = false)
  login
  @imap.select('INBOX')
  start_time = Time.now
  while Time.now - start_time < times
    get_emails_search_or_new(options).each do |message_id|
      mail = get_mail_data(message_id)
      string_found = mail[:subject].to_s.upcase.gsub(/\s+/, ' ')
      string_to_be_found = options[:subject].to_s.upcase.gsub(/\s+/, ' ')
      next unless string_found.include? string_to_be_found

      if move_out
        move_out_message(message_id)
      else
        mark_message_as_seen(message_id)
      end
      return mail
    end
  end
  nil
end
inspect() click to toggle source

@return [String] inspect info

# File lib/onlyoffice_iredmail_helper.rb, line 35
def inspect
  "IredMailHelper domain: #{@domainname}, " \
  "user: #{@username}, " \
  "subject: #{@subject}"
end
login() click to toggle source

Login to email via IMAP @return [nil]

# File lib/onlyoffice_iredmail_helper.rb, line 43
def login
  return if @imap

  @imap = Net::IMAP.new(@domainname)
  @imap.authenticate('LOGIN', @username, @password)
end
mail_by_subject(options = {}, times = 300) click to toggle source

Get email @param options [Hash] options of get @param times [Integer] count to check @return [Hash] mail

# File lib/onlyoffice_iredmail_helper.rb, line 102
def mail_by_subject(options = {}, times = 300)
  login
  @imap.select('INBOX')
  start_time = Time.now
  while Time.now - start_time < times
    get_emails_search_or_new(options).each do |message_id|
      mail = get_mail_data(message_id)
      mail_subject_found = mail[:subject].to_s.upcase
      mail_subject_to_be_found = options[:subject].to_s.upcase
      next unless mail_subject_found.include? mail_subject_to_be_found

      if options[:move_out]
        move_out_message(message_id)
      else
        mark_message_as_seen(message_id)
      end
      return mail
    end
  end
  nil
end
send_mail(options = {}) click to toggle source

Send mail @param options [Hash] hash with params @return [nil]

# File lib/onlyoffice_iredmail_helper.rb, line 68
def send_mail(options = {})
  options[:subject] ||= @default_subject
  options[:body] ||= @default_body
  options[:mailto] ||= @default_user
  smtp = Net::SMTP.start(@domainname, 25, @username, @username, @password, :login)
  smtp.send_message create_msg(options), @username, options[:mailto]
  smtp.finish
end

Private Instance Methods

close() click to toggle source

Correct close of mail account @return [Void]

# File lib/onlyoffice_iredmail_helper.rb, line 180
def close
  @imap.close
  @imap.logout
  @imap.disconnect
  @imap = nil
end
get_emails_search_or_new(options) click to toggle source

Get email list via search or all unseen @param options [Hash] options to search @return [Array<Mail>] list of mails

# File lib/onlyoffice_iredmail_helper.rb, line 204
def get_emails_search_or_new(options)
  if options[:search]
    @imap.search([(options[:search_type] || 'BODY').to_s.upcase, options[:search]])
  else
    @imap.search(['UNSEEN'])
  end
end
get_mail_data(message_id, search = nil) click to toggle source

Get mail data by mail id @param message_id [Integer] id of message @param search [String] param to search @return [Hash] found data

# File lib/onlyoffice_iredmail_helper.rb, line 216
def get_mail_data(message_id, search = nil)
  msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
  mail = Mail.read_from_string(msg)
  subject = mail.subject.force_encoding('utf-8')
  body = mail.text_part
  body = body.body.to_s.gsub(/\s+/, ' ').strip.force_encoding('utf-8') if body
  html_body = mail.html_part
  html_body = html_body.body.to_s.gsub(/\s+/, ' ').strip.force_encoding('utf-8') if html_body
  mark_message_as_seen(message_id, close_connection: false) unless search
  { subject: subject, body: body, html_body: html_body }
end
move_out_message(message_id) click to toggle source

Move out message to `checked` directory @param message_id [String] id of message @return [Void]

# File lib/onlyoffice_iredmail_helper.rb, line 190
def move_out_message(message_id)
  create_mailbox(@mailbox_for_archive) unless mailboxes.include?(@mailbox_for_archive)

  login
  @imap.select('INBOX')
  @imap.copy(message_id, @mailbox_for_archive)
  @imap.store(message_id, '+FLAGS', [:Deleted])
  @imap.expunge
  close
end
read_defaults() click to toggle source

Get S3 key and S3 private key @return [Array <String>] list of keys

# File lib/onlyoffice_iredmail_helper.rb, line 230
def read_defaults
  return if read_env_defaults

  yaml = YAML.load_file("#{Dir.home}/.gem-onlyoffice_iredmail_helper/config.yml")
  @default_domain = yaml['domain']
  @default_user = yaml['user']
  @default_password = yaml['password'].to_s
  @default_subject = yaml['subject']
rescue Errno::ENOENT
  raise Errno::ENOENT, 'No config found. Please create ~/.gem-onlyoffice_iredmail_helper/config.yml'
end
read_env_defaults() click to toggle source

Read keys from env variables

# File lib/onlyoffice_iredmail_helper.rb, line 243
def read_env_defaults
  return false unless ENV['IREDMAIL_PASSWORD']

  @default_domain = ENV['IREDMAIL_DOMAIN']
  @default_user = ENV['IREDMAIL_USER']
  @default_password = ENV['IREDMAIL_PASSWORD'].to_s
  @default_subject = ENV['IREDMAIL_SUBJECT']
end