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



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

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



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

def contact
  message.try(:contact)
end

#contact_email_addressObject



103
104
105
106
107
108
109
# File 'app/models/mail_manager/bounce.rb', line 103

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

#contact_full_nameObject



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

def contact_full_name
  contact.try(:full_name)
end

#delivery_error_codeObject



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

def delivery_error_code
  email.error_status
rescue
  nil
end

#delivery_error_messageObject



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

def delivery_error_message
  email.diagnostic_code
rescue
  nil
end

#dismissObject



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

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

#emailObject



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

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

#fail_addressObject



76
77
78
79
80
81
82
83
84
85
# File 'app/models/mail_manager/bounce.rb', line 76

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)


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

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

#mailing_subjectObject



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

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

#process(force = false) ⇒ Object

Parses email contents for bounce resolution



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/mail_manager/bounce.rb', line 37

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



62
63
64
# File 'app/models/mail_manager/bounce.rb', line 62

def reprocess
  process(true)
end

#subscriptionObject



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

def subscription
  message.try(:subscription)
end