Sha256: 25432cea67a2597dcf51e1d28b072605f5b5b518ac4304f3ca40770bc33fc16c

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module EffectivePostmarkMailer
  extend ActiveSupport::Concern

  included do
    # Make sure config.action_mailer.raise_delivery_errors = true
    before_action(unless: -> { Rails.env.test? || Rails.env.development? }) do
      unless (Rails.application.config.action_mailer.raise_delivery_errors rescue false)
        raise("Expected config.action_mailer.raise_delivery_errors to be true. Please update your environment for use with effective_postmark.")
      end
    end

    rescue_from ::Postmark::InactiveRecipientError, with: :effective_postmark_inactive_recipient_error
  end

  def effective_postmark_inactive_recipient_error(exception)
    # Read the current app's Tenant if defined
    tenant = if defined?(Tenant)
      Tenant.current || raise("Missing tenant in effective_postmark exception")
    end

    # Find the user to associate it with
    user_klass = (tenant ? Tenant.engine_user(tenant) : "User".safe_constantize)
    raise("Expected an effective_postmark_user") unless user_klass.try(:effective_postmark_user?)

    # All recipients
    recipients = (Array(exception.recipients) - [nil, "", " "]).map { |email| email.downcase.strip }

    # Find each user and mark them postmark_inactive and make a log
    recipients.each do |email|
      user = user_klass.find_for_database_authentication(email: email)

      if user.present?
        user.postmark_inactive_recipient!
      end

      ::EffectiveLogger.email("[ERROR] Inactive Recipient - #{email}", user: user, error_code: exception.error_code, message: exception.message)
    end

    true
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
effective_postmark-0.1.1 app/mailers/concerns/effective_postmark_mailer.rb