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



129
130
131
# File 'app/models/mail_manager/bounce.rb', line 129

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



100
101
102
# File 'app/models/mail_manager/bounce.rb', line 100

def contact
  message.try(:contact)
end

#contact_email_addressObject



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

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

#contact_full_nameObject



104
105
106
# File 'app/models/mail_manager/bounce.rb', line 104

def contact_full_name
  contact.try(:full_name)
end

#delivery_error_codeObject



116
117
118
119
120
# File 'app/models/mail_manager/bounce.rb', line 116

def delivery_error_code
  email.error_status
rescue
  nil
end

#delivery_error_messageObject



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

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



133
134
135
136
# File 'app/models/mail_manager/bounce.rb', line 133

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

#fail_addressObject



81
82
83
84
85
86
87
88
89
90
# 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)
    message.result = message.result.to_s + "Failed by Administrator: (bounced, not auto resolved) "
    message.change_status(:failed)
    change_status(:resolved)
  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



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

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(:resolved)
    elsif delivery_error_code =~ /5\.\d\.\d/ && delivery_error_message.present?
      transaction do 
        update_attribute(:comments, delivery_error_message)
        change_status(:resolved)
        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



96
97
98
# File 'app/models/mail_manager/bounce.rb', line 96

def subscription
  message.try(:subscription)
end