Class: MailManager::Bounce

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
StatusHistory
Defined in:
app/models/mail_manager/bounce.rb

Instance Method Summary collapse

Methods included from StatusHistory

#change_status, included, #set_default_status, #status, #status=, #status_changed_at=

Instance Method Details

#bounce_message_guidObject

Returns message guid



148
149
150
# File 'app/models/mail_manager/bounce.rb', line 148

def bounce_message_guid
  email.to_s.gsub(/^.*X-Bounce-GUID:\s*([^\s]+).*$/mi,'\1') if email.to_s.match(/X-Bounce-GUID:\s*([^\s]+)/mi)
end

#contactObject



119
120
121
# File 'app/models/mail_manager/bounce.rb', line 119

def contact
  message.try(:contact)
end

#contact_email_addressObject



127
128
129
130
131
132
133
# File 'app/models/mail_manager/bounce.rb', line 127

def contact_email_address
  if contact.blank?
    "Contact Deleted"
  else
    contact.email_address
  end
end

#contact_full_nameObject



123
124
125
# File 'app/models/mail_manager/bounce.rb', line 123

def contact_full_name
  contact.try(:full_name)
end

#delivery_error_codeObject



135
136
137
138
139
# File 'app/models/mail_manager/bounce.rb', line 135

def delivery_error_code
  email.error_status
rescue
  nil
end

#delivery_error_messageObject



141
142
143
144
145
# File 'app/models/mail_manager/bounce.rb', line 141

def delivery_error_message
  email.diagnostic_code
rescue
  nil
end

#dismissObject



92
93
94
95
96
# File 'app/models/mail_manager/bounce.rb', line 92

def dismiss
  raise "Status cannot be manually changed unless it needs manual intervention!" unless
    status.eql?('needs_manual_intervention')
  change_status(:dismissed)
end

#emailObject



152
153
154
155
# File 'app/models/mail_manager/bounce.rb', line 152

def email
  return @email if @email
  @email = Mail.new(bounce_message)
end

#fail_addressObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/mail_manager/bounce.rb', line 98

def fail_address
  raise "Status cannot be manually changed unless it needs manual intervention!"  unless
    status.eql?('needs_manual_intervention')
  transaction do 
    Subscription.fail_by_email_address(contact_email_address) if contact.present?
    if message.present?
      message.result = message.result.to_s + "Failed by Administrator: (bounced, not auto resolved) "
      message.change_status(:failed)
    end
    change_status(:removed)
  end
end

#from_mailer_daemon?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'app/models/mail_manager/bounce.rb', line 85

def from_mailer_daemon?
  ['postmaster','mailer-daemon'].include?(email.from.first.gsub(/\@.*$/,'').downcase)
rescue => e
  # they don't have a proper from email address
  delivery_error_code.present?
end

#mailing_subjectObject



111
112
113
# File 'app/models/mail_manager/bounce.rb', line 111

def mailing_subject
  message.try(:mailing).try(:subject)
end

#process(force = false) ⇒ Object

Parses email contents for bounce resolution



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
# File 'app/models/mail_manager/bounce.rb', line 42

def process(force=false)
  if status.eql?('unprocessed') || force
    self.message = Message.find_by_guid(bounce_message_guid)
    self.mailing = message.mailing unless message.nil?
    if email.subject.to_s =~ /unsubscribe/i
      guid = email.subject.to_s.gsub(/unsubscribe from (.*)/,'\1')
      self.message = Message.find_by_guid(guid)
      self.mailing = message.mailing unless message.nil?
      change_status(:unsubscribed)
      if(message.present?) 
        MailManager::Subscription.unsubscribe_by_email_address(contact_email_address)
      else
        email.from.each do |email_address|
          MailManager::Subscription.unsubscribe_by_email_address(email_address)
        end
      end
    elsif !from_mailer_daemon?
      change_status(:invalid)
    elsif delivery_error_code =~ /4\.\d\.\d/ || delivery_error_message.to_s =~ /quota/i
      update_attribute(:comments, delivery_error_message)
      change_status(:deferred)
    elsif delivery_error_code =~ /5\.\d\.\d/ && delivery_error_message.present?
      transaction do 
        update_attribute(:comments, delivery_error_message)
        change_status(:removed)
        if message.present?
          message.change_status(:failed)
          message.update_attribute(:result,"Failure Message from Bounce: #{delivery_error_message}")
        end
        Subscription.fail_by_email_address(contact_email_address)
      end
    else
      update_attribute(:comments, 'unrecognized diagnostic code')
      change_status(:needs_manual_intervention)
    end
    save
  end
end

#reprocessObject



81
82
83
# File 'app/models/mail_manager/bounce.rb', line 81

def reprocess
  process(true)
end

#subscriptionObject



115
116
117
# File 'app/models/mail_manager/bounce.rb', line 115

def subscription
  message.try(:subscription)
end