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



131
132
133
# File 'app/models/mail_manager/bounce.rb', line 131

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



102
103
104
# File 'app/models/mail_manager/bounce.rb', line 102

def contact
  message.try(:contact)
end

#contact_email_addressObject



110
111
112
113
114
115
116
# File 'app/models/mail_manager/bounce.rb', line 110

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

#contact_full_nameObject



106
107
108
# File 'app/models/mail_manager/bounce.rb', line 106

def contact_full_name
  contact.try(:full_name)
end

#delivery_error_codeObject



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

def delivery_error_code
  email.error_status
rescue
  nil
end

#delivery_error_messageObject



124
125
126
127
128
# File 'app/models/mail_manager/bounce.rb', line 124

def delivery_error_message
  email.diagnostic_code
rescue
  nil
end

#dismissObject



75
76
77
78
79
# File 'app/models/mail_manager/bounce.rb', line 75

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

#emailObject



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

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

#fail_addressObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/mail_manager/bounce.rb', line 81

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)


71
72
73
# File 'app/models/mail_manager/bounce.rb', line 71

def from_mailer_daemon?
  ['postmaster','mailer-daemon'].include?(email.from.first.gsub(/\@.*$/,'').downcase)
end

#mailing_subjectObject



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

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
# 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 !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)
        message.change_status(:failed)
        message.update_attribute(:result,"Failure Message from Bounce: #{delivery_error_message}")
        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



67
68
69
# File 'app/models/mail_manager/bounce.rb', line 67

def reprocess
  process(true)
end

#subscriptionObject



98
99
100
# File 'app/models/mail_manager/bounce.rb', line 98

def subscription
  message.try(:subscription)
end