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)
base.cattr_accessor :force_sanitize
base.force_sanitize = nil
base.cattr_accessor :sanitized_bcc
base.sanitized_bcc = nil
base.cattr_accessor :sanitized_cc
base.sanitized_cc = nil
base.cattr_accessor :sanitized_recipients
base.sanitized_recipients = nil
base.cattr_accessor :use_actual_email_as_sanitized_user_name
base.use_actual_email_as_sanitized_user_name = false
base.cattr_accessor :use_actual_email_prepended_to_subject
base.use_actual_email_prepended_to_subject = false
base.class_eval do
alias :real_bcc :bcc
alias :real_cc :cc
alias :real_recipients :recipients
alias :real_subject :subject
def localish?
!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
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
|