fluent-plugin-mail 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f383ef142caab20f8176e6c38e7cf774ed5d1a4d
4
- data.tar.gz: a5006e52c3e4a0fde6b7c713f245fdf44b5b07c9
3
+ metadata.gz: 481b1b97a2f2766802bcbd11bb3eb4d351e59fc6
4
+ data.tar.gz: 68d5a406f5cba6041b7086ced8234d40050685cf
5
5
  SHA512:
6
- metadata.gz: 81aca182ab3d260cbea3f57294a648770a22278d5fa525f69be6eb4448b1df75646faea58e7b3a1c77d100042314cae6a308cd1a9b1964c48e3e0414d8578a3c
7
- data.tar.gz: d412c15b6df8914e240b7491e5085b45031a5aaca8de4012d8d3833be7f442480545bfbd17b68fd81ae10e465b6a7e6d735ed9286e93b4fe3fe758d583dd629a
6
+ metadata.gz: d934bc3e3e385f876ef068a213738f28a752046a24922bb76f69ba684d820a138f23d7daedc2d134815586165aeaf7b3447740878f206617f31ce700d983de5e
7
+ data.tar.gz: fb72bf8db210ef6e3606583b5446a1a5b284c77349e9167e7f04f115ccdaa0306c8358388cae11959b2ca419404b320fa9d39702915fef81cc2e6d6213a1a9c3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.2.4
2
+
3
+ Enhancements:
4
+
5
+ * Add to_key, cc_key, bcc_key options (thanks to authorNari)
6
+
1
7
  # 0.2.3
2
8
 
3
9
  Enhancements:
data/README.md CHANGED
@@ -205,10 +205,23 @@ log server("/etc/td-agent/td-agent.conf")
205
205
  </match>
206
206
 
207
207
 
208
+ ### Dynamic identiciation of mail destination (to, cc, bcc)
208
209
 
209
- ## TODO
210
+ You can dynamically identify mail destination (to, cc, bcc) from event records as:
210
211
 
211
- * add config "mail_text_format"
212
+
213
+ ```
214
+ <match mail.test>
215
+ type mail
216
+ from from@example.com
217
+ to_key to
218
+ cc_key cc
219
+ bcc_key bcc
220
+ ....
221
+ </match>
222
+ ```
223
+
224
+ With this example, `to`, `cc`, `bcc` are dynamically extracted from `record["to"]`, `record["cc"]`, `record["bcc"]` respectively.
212
225
 
213
226
  ## ChangeLog
214
227
 
@@ -228,7 +241,7 @@ It starts STMP server on localhost:1025, and has Web UI running on localhost:108
228
241
  Run Fluentd as:
229
242
 
230
243
  ```
231
- $ bundle exec fluentd -c example.conf
244
+ $ bundle exec fluentd -c example/example.conf
232
245
  ```
233
246
 
234
247
  Put a message to the Fluentd as:
@@ -1,3 +1,5 @@
1
+ # echo '{"message":"This is a test"}' | bundle exec fluent-cat mail.test
2
+
1
3
  <source>
2
4
  type forward
3
5
  </source>
@@ -7,9 +9,9 @@
7
9
  log_level debug
8
10
  host localhost
9
11
  port 1025
10
- from foo@example.com
11
- to bar@example.com
12
- bcc baz@example.com
12
+ from from@example.com
13
+ to to@example.com
14
+ bcc bcc@example.com
13
15
  subject this is a test
14
16
  message Hello from out_mail\n[%s] %s
15
17
  message_out_keys time,message
@@ -0,0 +1,23 @@
1
+ # echo '{"message":"This is a test","bcc":"bcc@example.com"}' | bundle exec fluent-cat mail.test
2
+
3
+ <source>
4
+ type forward
5
+ </source>
6
+
7
+ <match mail.test>
8
+ type mail
9
+ log_level debug
10
+ host localhost
11
+ port 1025
12
+ from from@example.com
13
+ to to@example.com
14
+ to_key to
15
+ bcc_key bcc
16
+ subject this is a test
17
+ message Hello from out_mail\n[%s] %s
18
+ message_out_keys time,message
19
+ time_key time
20
+ time_locale Asia/Taipei
21
+ # localtime false
22
+ time_format %Y-%m-%d %H:%M:%S %z
23
+ </match>
@@ -6,13 +6,12 @@ Gem::Specification.new do |gem|
6
6
  gem.description = %q{output plugin for Mail}
7
7
  gem.summary = %q{output plugin for Mail}
8
8
  gem.homepage = "https://github.com/u-ichi/fluent-plugin-mail"
9
- gem.rubyforge_project = "fluent-plugin-mail"
10
9
  gem.files = `git ls-files`.split($\)
11
10
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
11
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
12
  gem.name = "fluent-plugin-mail"
14
13
  gem.require_paths = ["lib"]
15
- gem.version = '0.2.3'
14
+ gem.version = '0.2.4'
16
15
  gem.license = "Apache-2.0"
17
16
 
18
17
  gem.add_runtime_dependency "fluentd"
@@ -42,8 +42,14 @@ class Fluent::MailOutput < Fluent::Output
42
42
  config_param :to, :string, :default => ''
43
43
  desc "Mail destination (Cc)"
44
44
  config_param :cc, :string, :default => ''
45
- desc "Mail destination (BCc)"
45
+ desc "Mail destination (Bcc)"
46
46
  config_param :bcc, :string, :default => ''
47
+ desc "Dyanmically identify mail destination (To) from records"
48
+ config_param :to_key, :string, :default => nil
49
+ desc "Dynamically identify mail destination (Cc) from records"
50
+ config_param :cc_key, :string, :default => nil
51
+ desc "Dynamically identify mail destination (Bcc) from records"
52
+ config_param :bcc_key, :string, :default => nil
47
53
  desc "Format string to construct mail subject"
48
54
  config_param :subject, :string, :default => 'Fluent::MailOutput plugin'
49
55
  desc "Specify comma delimited keys output to `subject`"
@@ -90,6 +96,35 @@ class Fluent::MailOutput < Fluent::Output
90
96
  @create_message_proc = Proc.new {|tag, time, record| create_key_value_message(tag, time, record) }
91
97
  end
92
98
 
99
+ if @to_key or @cc_key or @bcc_key
100
+ @process_event_stream_proc = Proc.new {|tag, es|
101
+ messages = []
102
+ subjects = []
103
+ dests = []
104
+
105
+ es.each do |time, record|
106
+ messages << @create_message_proc.call(tag, time, record)
107
+ subjects << create_formatted_subject(tag, time, record)
108
+ dests << %w(to cc bcc).each_with_object({}){|t, dest| dest[t] = create_dest_addr(t, record) }
109
+ end
110
+
111
+ [messages, subjects, dests]
112
+ }
113
+ else
114
+ @process_event_stream_proc = Proc.new {|tag, es|
115
+ messages = []
116
+ subjects = []
117
+ dests = []
118
+
119
+ es.each do |time, record|
120
+ messages << @create_message_proc.call(tag, time, record)
121
+ subjects << create_formatted_subject(tag, time, record)
122
+ end
123
+
124
+ [messages, subjects, dests]
125
+ }
126
+ end
127
+
93
128
  begin
94
129
  @subject % (['1'] * @subject_out_keys.length)
95
130
  rescue ArgumentError
@@ -104,19 +139,13 @@ class Fluent::MailOutput < Fluent::Output
104
139
  end
105
140
 
106
141
  def emit(tag, es, chain)
107
- messages = []
108
- subjects = []
142
+ messages, subjects, dests = @process_event_stream_proc.call(tag, es)
109
143
 
110
- es.each {|time,record|
111
- messages << @create_message_proc.call(tag, time, record)
112
- subjects << create_formatted_subject(tag, time, record)
113
- }
114
-
115
- (0...messages.size).each do |i|
116
- message = messages[i]
144
+ messages.each_with_index do |message, i|
117
145
  subject = subjects[i]
146
+ dest = dests[i]
118
147
  begin
119
- sendmail(subject, message)
148
+ sendmail(subject, message, dest)
120
149
  rescue => e
121
150
  log.warn "out_mail: failed to send notice to #{@host}:#{@port}, subject: #{subject}, message: #{message}, " <<
122
151
  "error_class: #{e.class}, error_message: #{e.message}, error_backtrace: #{e.backtrace.first}"
@@ -179,7 +208,7 @@ class Fluent::MailOutput < Fluent::Output
179
208
  @subject % values
180
209
  end
181
210
 
182
- def sendmail(subject, msg)
211
+ def sendmail(subject, msg, dest = nil)
183
212
  smtp = Net::SMTP.new(@host, @port)
184
213
 
185
214
  if @user and @password
@@ -193,6 +222,9 @@ class Fluent::MailOutput < Fluent::Output
193
222
 
194
223
  subject = subject.force_encoding('binary')
195
224
  body = msg.force_encoding('binary')
225
+ to = (dest && dest['to']) ? dest['to'] : @to
226
+ cc = (dest && dest['cc']) ? dest['cc'] : @cc
227
+ bcc = (dest && dest['bcc']) ? dest['bcc'] : @bcc
196
228
 
197
229
  # Date: header has timezone, so usually it is not necessary to set locale explicitly
198
230
  # But, for people who would see mail header text directly, the locale information may help something
@@ -203,9 +235,9 @@ class Fluent::MailOutput < Fluent::Output
203
235
  content = <<EOF
204
236
  Date: #{date}
205
237
  From: #{@from}
206
- To: #{@to}
207
- Cc: #{@cc}
208
- Bcc: #{@bcc}
238
+ To: #{to}
239
+ Cc: #{cc}
240
+ Bcc: #{bcc}
209
241
  Subject: #{subject}
210
242
  Message-Id: #{mid}
211
243
  Mime-Version: 1.0
@@ -213,7 +245,7 @@ Content-Type: #{@content_type}
213
245
 
214
246
  #{body}
215
247
  EOF
216
- response = smtp.send_mail(content, @from, @to.split(/,/), @cc.split(/,/), @bcc.split(/,/))
248
+ response = smtp.send_mail(content, @from, to.split(/,/), cc.split(/,/), bcc.split(/,/))
217
249
  log.debug "out_mail: content: #{content.gsub("\n", "\\n")}"
218
250
  log.debug "out_mail: email send response: #{response.string.chomp}"
219
251
  smtp.finish
@@ -245,4 +277,14 @@ EOF
245
277
  retry
246
278
  end
247
279
  end
280
+
281
+ def create_dest_addr(dest_type, record)
282
+ addr = instance_variable_get(:"@#{dest_type}")
283
+ dest_key = instance_variable_get(:"@#{dest_type}_key")
284
+ if dest_key
285
+ return record[dest_key] || addr
286
+ else
287
+ return addr
288
+ end
289
+ end
248
290
  end
@@ -46,6 +46,21 @@ class MailOutputTest < Test::Unit::TestCase
46
46
  to localhost@localdomain
47
47
  ]
48
48
 
49
+ CONFIG_DEST_ADDR = %[
50
+ out_keys tag,time,value
51
+ time_key time
52
+ time_format %Y/%m/%d %H:%M:%S
53
+ tag_key tag
54
+ subject Fluentd Notification Alarm %s
55
+ subject_out_keys tag
56
+ host localhost
57
+ port 25
58
+ from localhost@localdomain
59
+ to_key to
60
+ cc_key cc
61
+ bcc_key bcc
62
+ ]
63
+
49
64
  def create_driver(conf=CONFIG_OUT_KEYS,tag='test')
50
65
  Fluent::Test::OutputTestDriver.new(Fluent::MailOutput, tag).configure(conf)
51
66
  end
@@ -75,6 +90,20 @@ class MailOutputTest < Test::Unit::TestCase
75
90
  end
76
91
  end
77
92
 
93
+ def test_dest_addr
94
+ d = create_driver(CONFIG_DEST_ADDR)
95
+ time = Time.now.to_i
96
+ d.run do
97
+ d.emit({
98
+ 'value' => "message mail from fluentd out_mail",
99
+ 'to' => "localhost@localdomain",
100
+ 'cc' => "localhost@localdomain",
101
+ 'bcc' => "localhost@localdomain",
102
+ },
103
+ time)
104
+ end
105
+ end
106
+
78
107
  def test_with_scrub
79
108
  d = create_driver(CONFIG_MESSAGE)
80
109
  invalid_string = "\xff".force_encoding('UTF-8')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-mail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuichi UEMURA
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-12-29 00:00:00.000000000 Z
12
+ date: 2016-03-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fluentd
@@ -69,7 +69,8 @@ files:
69
69
  - LICENSE
70
70
  - README.md
71
71
  - Rakefile
72
- - example.conf
72
+ - example/example.conf
73
+ - example/to_cc_bcc_key.conf
73
74
  - fluent-plugin-mail.gemspec
74
75
  - lib/fluent/plugin/out_mail.rb
75
76
  - test/helper.rb
@@ -93,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
94
  - !ruby/object:Gem::Version
94
95
  version: '0'
95
96
  requirements: []
96
- rubyforge_project: fluent-plugin-mail
97
+ rubyforge_project:
97
98
  rubygems_version: 2.5.1
98
99
  signing_key:
99
100
  specification_version: 4