Class: MailClient

Inherits:
Object
  • Object
show all
Extended by:
MailgunHelper
Defined in:
lib/howitzer/utils/email/mail_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MailgunHelper

create_mailbox, delete_all_mailboxes, delete_mailbox

Class Method Details

.by_email(name) ⇒ Object



18
19
20
21
22
23
# File 'lib/howitzer/utils/email/mail_client.rb', line 18

def self.by_email(name)
  log.info "Connect to '#{name}' mailbox"
  options = self.merge_opts(pop3: { user_name: name }, smtp: {})
  @clients[options] = new(options) unless @clients.has_key?(options)
  @clients[options]
end

.defaultObject



11
12
13
14
15
16
# File 'lib/howitzer/utils/email/mail_client.rb', line 11

def self.default
  log.info "Connect to default mailbox"
  options = merge_opts
  @clients[options] = new unless @clients.has_key?(options)
  @clients[options]
end

.merge_opts(opts = {smtp: {}, pop3: {}}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/howitzer/utils/email/mail_client.rb', line 25

def self.merge_opts(opts={smtp: {}, pop3: {}})
  def_smtp_opts = {
      address: settings.mail_smtp_server,
      port: settings.mail_smtp_port,
      domain: settings.mail_smtp_domain,
      user_name: settings.mail_smtp_user_name,
      password: settings.mail_smtp_user_pass,
      authentication: 'plain',
      enable_starttls_auto: true
  }

  def_pop3_opts = {
      address: settings.mail_pop3_server,
      port: settings.mail_pop3_port,
      user_name: settings.mail_pop3_user_name,
      password: settings.mail_pop3_user_pass
  }
  {
      smtp: def_smtp_opts.merge(opts[:smtp]),
      pop3: def_pop3_opts.merge(opts[:pop3])
  }
end

Instance Method Details

#empty_inboxObject



106
107
108
109
110
111
112
113
# File 'lib/howitzer/utils/email/mail_client.rb', line 106

def empty_inbox
  begin
    start {|pop_obj| pop_obj.delete_all }
    log.info "Email box (#{@options[:pop3][:user_name]}) was purged"
  rescue Net::POPError => e
    log.warn "Exception during deletion all messages from '#{@options[:pop3][:user_name]}':\n#{e.message}"
  end
end

#find_mail(max_wait = settings.mail_pop3_timeout, keep_or_delete = :delete, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/howitzer/utils/email/mail_client.rb', line 48

def find_mail(max_wait = settings.mail_pop3_timeout, keep_or_delete = :delete, &block)
  messages = []
  time_of_start = Time.now
  exception = nil
  while Time.now < time_of_start + max_wait
    begin
      exception = nil
      start do |pop_obj|
        pop_obj.mails.each do |mail|
          begin
            mail_message = Mail.new(mail.pop.to_s)
            if block_given?
              if block.call(mail_message)
                if keep_or_delete == :delete
                  mail.delete
                  log.info "Mail '#{mail_message.subject}' deleted from #{@options[:pop3][:user_name]}"
                end
                messages << mail_message
                @old_ids << mail.unique_id
                return messages
              end
            else
              messages << mail_message
            end
          rescue Net::POPError => e
            log.warn "Exception in POP find_mail: #{e.message}"
            exception = e
          end
        end
      end

      sleep 5
    rescue Errno::ETIMEDOUT, Errno::ECONNRESET, Net::POPAuthenticationError => e
      log.warn "Exception in POP find_mail: #{e.message}. Will retry."
      exception = e
    end
  end
  log.error exception unless exception.nil?
  messages
end

#send_mail(mail_from, mail_to, mail_subject, mail_body, mail_attachment = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/howitzer/utils/email/mail_client.rb', line 89

def send_mail(mail_from, mail_to, mail_subject, mail_body, mail_attachment=nil)
  log.info "Send emails with next parameters:\n " +
               "mail_from : #{mail_from},\n mail_to : #{mail_to}\n " +
               "mail_subject : #{mail_subject},\n has_attachment? : #{!mail_attachment.nil?}"
  outbound_mail = Mail.new do
    from(mail_from)
    subject(mail_subject)
    body(mail_body)
    add_file(mail_attachment) unless mail_attachment.nil?
  end
  outbound_mail[:to] = mail_to

  retryable(:on => [Errno::ETIMEDOUT, Timeout::Error], :sleep => 10, :tries => 3, :trace => true, :logger => log) do
    outbound_mail.deliver!
  end
end

#start(&block) ⇒ Object



115
116
117
118
119
120
# File 'lib/howitzer/utils/email/mail_client.rb', line 115

def start(&block)
  retryable(:timeout => settings.mail_pop3_timeout, :sleep => 5, :trace => true, :logger => log) do
    Net::POP3.start(@options[:pop3][:address], @options[:pop3][:port],
                  @options[:pop3][:user_name], @options[:pop3][:password], &block)
  end
end