Class: Smailer::Tasks::ProcessBounces

Inherits:
Object
  • Object
show all
Defined in:
lib/smailer/tasks/process_bounces.rb

Constant Summary collapse

@@keys_messages =
{}
@@bounce_counts =
Hash.new(0)
@@emails_bounces =
{}
@@unsubscribed =
{}
@@test_mode =
false

Class Method Summary collapse

Class Method Details

.execute(options = {}) ⇒ Object

You need to provide at least a :server, :username and a :password options to execute(). These will represent the POP3 connection details to the bounce mailbox which will be processed. Also consider providing a concrete implementation to the :subscribed_checker option, so that bounces for unknown or already-unsubscribed emails do not remain clogging-up the mailbox Example usage:

Smailer::Tasks::ProcessBounces.execute({

:server           => 'bounces.example.org',
:username         => '[email protected]',
:password         => 'somesecret',
:subscribed_checker => lambda { |recipient| Subscribers.subscribed.where(:email => recipient).first.present? },

}) do |unsubscribe_details|

Subscribers.where(:email => unsubscribe_details[:recipient]).destroy

end



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/smailer/tasks/process_bounces.rb', line 28

def execute(options = {})
  deleted = 0
  options = {
    :port => 110,
    :subscribed_checker => lambda { true },
    :bounce_counts_per_type_to_unsubscribe => {
      1   => ['5.1.1', '5.1.2', '5.1.3', '5.1.6', '5.1.8', '5.7.1'],
      3   => ['5.2.1', '5.1.0'],
      5   => ['5.5.0'],
      8   => ['5.0.0'],
      12  => ['5.2.2', '5.2.0', '5.3.0', '5.3.2'],
      16  => ['4.2.2', '4.4.2', '4.0.0'],
      30  => ['97', '98', '99'],
    },
    :default_bounce_count_per_type_to_unsubscribe => 20,
    :total_bounce_count_to_unsubscribe => 40,
    :delete_unprocessed_bounces => true,
    :logger => lambda { |msg| puts msg },
  }.merge(options)

  logger = lambda do |msg|
    options[:logger].call msg if options[:logger] && options[:logger].respond_to?(:call)
  end

  connect_to_mailbox(options) do |pop|
    mails_to_process = pop.mails.size
    logger.call "#{mails_to_process} mail(s) to process"

    pop.mails.each_with_index do |m, i|
      logger.call "#{((i + 1) * 100.0 / mails_to_process).round(2)}% (#{i + 1} of #{mails_to_process})" if i % 200 == 0

      mail      = Mail.new m.pop
      bounce    = BounceEmail::Mail.new mail
      processed = false

      if bounce.bounced?
        original_email = sent_message_from_bounce(mail)

        if original_email
          recipient = original_email.to

          if !unsubscribed?(recipient) && options[:subscribed_checker].call(recipient)
            processed = true
            register_bounce recipient, bounce, m

            bounce_count_per_type = bounce_count_for recipient, bounce.code
            total_bounce_count    = bounce_count_for recipient

            rule = options[:bounce_counts_per_type_to_unsubscribe].select { |count, codes| codes.include?(bounce.code) }.first
            max_count_per_type = rule ? rule.first : options[:default_bounce_count_per_type_to_unsubscribe]

            bounce_count_per_type_exceeded = bounce_count_per_type >= max_count_per_type
            total_bounce_count_exceeded    = total_bounce_count >= options[:total_bounce_count_to_unsubscribe]

            if bounce_count_per_type_exceeded || total_bounce_count_exceeded
              logger.call "=> Unsubscribing #{recipient} and deleting #{bounces_for(recipient).size} bounced email(s)"

              yield :recipient => recipient, :original_email => original_email, :bounce => bounce
              unsubscribe recipient
            end
          end
        end
      end

      if options[:delete_unprocessed_bounces] && !processed
        m.delete unless test_mode?
        deleted += 1
      end
    end
  end

  logger.call "Deleted #{deleted} unprocessed bounce(s) from the mailbox" if deleted > 0
  connect_to_mailbox(options) { |pop| logger.call "Messages left in the mailbox: #{pop.mails.size}" }
end

.test_mode=(mode) ⇒ Object



107
108
109
# File 'lib/smailer/tasks/process_bounces.rb', line 107

def test_mode=(mode)
  @@test_mode = mode
end

.test_mode?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/smailer/tasks/process_bounces.rb', line 103

def test_mode?
  @@test_mode
end