Class: UnsubscribesMailbox

Inherits:
ApplicationMailbox show all
Defined in:
app/mailboxes/unsubscribes_mailbox.rb

Constant Summary collapse

RECIPIENT_FORMAT =

E.g list-unsubscribe+79b3b15c-4f68-4d53-bf32-b44966744c45@warmlyyours.com
list-remove+8b3ee4d1-6c53-4c7b-b654-66f4328030ce@warmlyyours.com>
See communication mailer where this is set into the header in #email method
If this format changes then it needs to change there as well

/list-(remove|unsubscribe)\+([\w-]+)@/i

Constants inherited from ApplicationMailbox

ApplicationMailbox::DIRECT_FILE_EXTENSIONS, ApplicationMailbox::FETCHABLE_MIME_TYPES, ApplicationMailbox::FETCH_MAX_SIZE, ApplicationMailbox::GDRIVE_FILE_RE, ApplicationMailbox::GDRIVE_OPEN_RE, ApplicationMailbox::MAX_FILENAME_LENGTH

Instance Method Summary collapse

Methods inherited from ApplicationMailbox

#attempt_url_download, #collect_fetchable_urls, #create_activity, #create_communication, #fetch_linked_files, #find_sender_party, #get_resource_id, #inline_or_attachment_part?, #process_attachments, #process_content, #replace_cid_references, #sanitize_attachment_filename

Instance Method Details

#processObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/mailboxes/unsubscribes_mailbox.rb', line 7

def process
  to = mail.to&.first
  sender = mail.from&.first
  # subject = mail.subject&.first
  # note = mail.decoded

  Rails.logger.info "Processing Unbuscribe Mail for #{sender}, in response to #{to}"

  # Parse the destination id
  uid = to.scan(RECIPIENT_FORMAT)&.first&.first
  if uid.present?
    Rails.logger.info "Parsed uid as #{uid}, seeking emails matching"
    emails = CommunicationRecipient.emails.joins(:communication).where(communications: { unique_id: uid }).where.not(detail: nil).emails.distinct.pluck(:detail).map(&:downcase)
    # If we have a specific match on the sender, we will unsub that email only, otherwise every email
    if (parsed_emails = emails.select { |e| e == sender&.downcase }).present?
      Rails.logger.info "Detected #{parsed_emails.join(', ')} and unsubscribing"
      emails = parsed_emails
    else
      Rails.logger.info "Unsubscribing all emails: #{emails.join(', ')}"
    end
    PaperTrail.request(whodunnit: 'UnsubscribesMailbox') do
      EmailPreference.unsubscribe(emails)
    end
  else
    msg = "UID could not be extracted for sender #{sender} with unsubscribe #{to} process manually"
    Rails.logger.error
    ErrorReporting.error msg
  end
end