Sha256: 20b09b07273fabab71479f669ab357c965f99300f683176f26ac17d7a9e0e2e4

Contents?: true

Size: 1.28 KB

Versions: 20

Compression:

Stored size: 1.28 KB

Contents

module EffectiveLogging
  class EmailLogger
    def self.delivering_email(message)
      return unless message.present?

      # collect a Hash of arguments used to invoke EffectiveLogger.success
      logged_fields = { from: message.from.join(','), to: message.to, subject: message.subject }

      # Add a log header to your mailer to pass some objects or additional things to EffectiveLogger
      # mail(to: 'admin@example.com', subject: @post.title, log: { post: @post })
      if message.header['log'].present?
        # This is a bit sketchy, but gives access to the object in Rails 4.2 anyway
        logged_fields.merge!(message.header['log'].instance_variable_get(:@value) || {})

        # Get rid of the extra header, as it should not be set in the real mail message.
        message.header['log'] = nil
      end

      body = (message.body.try(:parts) || []).find { |part| part.content_type.to_s.downcase.include?('text/html') }

      logged_fields[:email] = "#{message.header}<hr>#{(body.presence || message.body)}"

      (message.to || []).each do |to|
        logged_fields[:to] = to
        logged_fields[:associated] ||= (User.where(email: to).first rescue nil)

        ::EffectiveLogger.email("#{message.subject} - #{message.to.join(', ')}", logged_fields)
      end
    end

  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
effective_logging-1.11.0 lib/effective_logging/email_logger.rb
effective_logging-1.10.18 lib/effective_logging/email_logger.rb
effective_logging-1.10.17 lib/effective_logging/email_logger.rb
effective_logging-1.10.16 lib/effective_logging/email_logger.rb
effective_logging-1.10.15 lib/effective_logging/email_logger.rb
effective_logging-1.10.14 lib/effective_logging/email_logger.rb
effective_logging-1.10.13 lib/effective_logging/email_logger.rb
effective_logging-1.10.12 lib/effective_logging/email_logger.rb
effective_logging-1.10.11 lib/effective_logging/email_logger.rb
effective_logging-1.10.10 lib/effective_logging/email_logger.rb
effective_logging-1.10.9 lib/effective_logging/email_logger.rb
effective_logging-1.10.8 lib/effective_logging/email_logger.rb
effective_logging-1.10.7 lib/effective_logging/email_logger.rb
effective_logging-1.10.6 lib/effective_logging/email_logger.rb
effective_logging-1.10.5 lib/effective_logging/email_logger.rb
effective_logging-1.10.4 lib/effective_logging/email_logger.rb
effective_logging-1.10.3 lib/effective_logging/email_logger.rb
effective_logging-1.10.2 lib/effective_logging/email_logger.rb
effective_logging-1.10.1 lib/effective_logging/email_logger.rb
effective_logging-1.10.0 lib/effective_logging/email_logger.rb