Class: EffectiveLogging::EmailLogger
- Inherits:
-
Object
- Object
- EffectiveLogging::EmailLogger
- Defined in:
- lib/effective_logging/email_logger.rb
Class Method Summary collapse
Class Method Details
.delivering_email(message) ⇒ Object
3 4 5 6 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 36 37 38 39 40 41 42 |
# File 'lib/effective_logging/email_logger.rb', line 3 def self.delivering_email() return if EffectiveLogging.supressed? return unless .present? # collect a Hash of arguments used to invoke EffectiveLogger.success fields = { from: .from.join(','), to: .to, subject: .subject, cc: .cc, bcc: .bcc } # Add a log header to your mailer to pass some objects or additional things to EffectiveLogger # mail(to: '[email protected]', subject: @post.title, log: { post: @post }) if .header['log'].present? # This is a bit sketchy, but gives access to the object in Rails 4.2 anyway fields.merge!(.header['log'].instance_variable_get(:@value) || {}) # Get rid of the extra header, as it should not be set in the real mail message. .header['log'] = nil end # Pass a tenant to your mailer # mail(to: '[email protected]', subject: @post.title, tenant: Tenant.current) tenant = if .header['tenant'].present? value = .header['tenant'].to_s.to_sym # OptionalField, not a String here .header['tenant'] = nil value end || (Tenant.current if defined?(Tenant)) user_klass = "#{tenant.to_s.classify}::User".safe_constantize body = (.body.try(:parts) || []).find { |part| part.content_type.to_s.downcase.include?('text/html') } body ||= .body fields[:email] = "#{message.header}<hr>#{body}" if tenant.present? Tenant.as(tenant) { log_email(, fields, user_klass) } else log_email(, fields, user_klass) end true end |
.log_email(message, fields, user_klass) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/effective_logging/email_logger.rb', line 46 def self.log_email(, fields, user_klass) tos = Array(.to) - [nil, ''] tos.each do |to| user = (user_klass.where(email: to.downcase).first if user_klass.present?) user_fields = fields.merge(to: to, user: user, associated: user) ::EffectiveLogger.email("#{message.subject} - #{tos.join(', ')}", user_fields) end if tos.blank? && (.cc.present? || .bcc.present?) ::EffectiveLogger.email("#{message.subject} - multiple recipients", fields) end end |