Sha256: 405b9e9e0172b71a88d000098caad65abca3b534087175d3eeadc3158554535b

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 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: { associated: @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

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

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

        EffectiveLogger.success("email sent: #{message.subject}", logged_fields)
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
effective_logging-1.5.2 lib/effective_logging/email_logger.rb
effective_logging-1.5.1 lib/effective_logging/email_logger.rb
effective_logging-1.5.0 lib/effective_logging/email_logger.rb
effective_logging-1.4.1 lib/effective_logging/email_logger.rb
effective_logging-1.4.0 lib/effective_logging/email_logger.rb