Module: NinthBit::SanitizeEmail

Defined in:
lib/sanitize_email/sanitize_email.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sanitize_email/sanitize_email.rb', line 7

def self.included(base)

  # Adds the following class attributes to the classes that include NinthBit::SanitizeEmail
  base.cattr_accessor :force_sanitize
  base.force_sanitize = nil

  # Specify the BCC addresses for the messages that go out in 'local' environments
  base.cattr_accessor :sanitized_bcc
  base.sanitized_bcc = nil

  # Specify the CC addresses for the messages that go out in 'local' environments
  base.cattr_accessor :sanitized_cc
  base.sanitized_cc = nil

  # The recipient addresses for the messages, either as a string (for a single
  # address) or an array (for multiple addresses) that go out in 'local' environments
  base.cattr_accessor :sanitized_recipients
  base.sanitized_recipients = nil

  # Use the 'real' email address as the username for the sanitized email address
  # e.g. "[email protected] <[email protected]>"
  base.cattr_accessor :use_actual_email_as_sanitized_user_name
  base.use_actual_email_as_sanitized_user_name = false

  # Prepend the 'real' email address onto the Subject line of the message
  # e.g. "[email protected] rest of subject"
  base.cattr_accessor :use_actual_email_prepended_to_subject
  base.use_actual_email_prepended_to_subject = false

  base.class_eval do
    # We need to alias these methods so that our new methods get used instead
    alias :real_bcc :bcc
    alias :real_cc :cc
    alias :real_recipients :recipients
    alias :real_subject :subject

    def localish?
      # consider_local is a method in sanitize_email/lib/custom_environments.rb
      # it is included in ActionMailer in sanitize_email/init.rb
      !self.class.force_sanitize.nil? ? self.class.force_sanitize : self.class.consider_local?
    end

    def subject(*lines)
      real_subject(*lines)
      localish? ? override_subject : real_subject
    end

    def recipients(*addresses)
      real_recipients(*addresses)
      if localish?
        puts "sanitize_email error: sanitized_recipients is not set" if self.class.sanitized_recipients.nil?
        override_email(:recipients)
      else
        real_recipients
      end
    end

    def cc(*addresses)
      real_cc(*addresses)
      localish? ? override_email(:cc) : real_cc
    end

    def bcc(*addresses)
      real_bcc(*addresses)
      localish? ? override_email(:bcc) : real_bcc
    end

    #######
    private
    #######

    def override_subject
      real_recipients.nil? ? real_subject : "Sanitized: [#{RAILS_ENV.upcase}](#{real_recipients}) #{real_subject}"
    end

    def override_email(type)
      real_addresses, sanitized_addresses =
        case type
        when :recipients
          [real_recipients, self.class.sanitized_recipients]
        when :cc
          [real_cc, self.class.sanitized_cc]
        when :bcc
          [real_bcc, self.class.sanitized_bcc]
        else raise "sanitize_email error: unknown email override"
        end

      # If there were no original recipients, then we DO NOT override the nil with the sanitized recipients
      return nil if real_addresses.nil?
      return sanitized_addresses if sanitized_addresses.nil? || !self.class.use_actual_email_as_sanitized_user_name

      out = real_addresses.inject([]) do |result, real_recipient|
        result << sanitized_addresses.map{|sanitized| "#{real_recipient} <#{sanitized}>"}
        result
      end.flatten
      return out
    end

  end
end