Class: Log4r::MailOutputter

Inherits:
Outputter
  • Object
show all
Defined in:
lib/log4r/mail_outputter.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_name, hash = {}) ⇒ MailOutputter

Returns a new instance of MailOutputter.



10
11
12
13
14
15
16
17
18
19
# File 'lib/log4r/mail_outputter.rb', line 10

def initialize(_name, hash={})
  super(_name, hash)
  @host = hash[:host] || hash["host"] || "localhost"
  @port = hash[:port] || hash["port"] || 25
  @from = hash[:from] || hash["from"] || ""
  @to = hash[:to] || hash["to"] || ""
  @subject = hash[:subject] || hash["subject"] || ""
  @body = hash[:body] || hash["body"] || ""
  @encoding = hash[:encoding] || hash["encoding"] || "UTF-8"
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



9
10
11
# File 'lib/log4r/mail_outputter.rb', line 9

def body
  @body
end

#encodingObject

Returns the value of attribute encoding.



9
10
11
# File 'lib/log4r/mail_outputter.rb', line 9

def encoding
  @encoding
end

#fromObject

Returns the value of attribute from.



9
10
11
# File 'lib/log4r/mail_outputter.rb', line 9

def from
  @from
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/log4r/mail_outputter.rb', line 9

def host
  @host
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/log4r/mail_outputter.rb', line 9

def port
  @port
end

#subjectObject

Returns the value of attribute subject.



9
10
11
# File 'lib/log4r/mail_outputter.rb', line 9

def subject
  @subject
end

#toObject

Returns the value of attribute to.



9
10
11
# File 'lib/log4r/mail_outputter.rb', line 9

def to
  @to
end

Instance Method Details

#add_files(mail, params) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/log4r/mail_outputter.rb', line 53

def add_files(mail, params)
  files = params[:files] || params["files"]
  return if !files
  files.each do |file|
    mail.add_file(file)
  end
end

#apply_encoding(encoding, content) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/log4r/mail_outputter.rb', line 61

def apply_encoding(encoding, content)
  if encoding == "ISO-2022-JP"
    ::NKF.nkf("-j", content).force_encoding("binary")
  else
    content
  end
end

#make_mail(params) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/log4r/mail_outputter.rb', line 36

def make_mail(params)
  encoding = params[:encoding] || params["encoding"] || @encoding
  from = params[:from] || params["from"] || @from
  to = params[:to] || params["to"] || @to
  subject = apply_encoding(encoding, params[:subject] || params["subject"] || @subject)
  body = apply_encoding(encoding, params[:body] || params["body"] || @body)
  
  mail = ::Mail.new do
    from from
    to to
    subject subject
    body body
  end
  mail.charset = encoding
  mail
end

#send_mail(params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/log4r/mail_outputter.rb', line 25

def send_mail(params)
  mail = make_mail(params)
  add_files(mail, params)
  mail.delivery_method :smtp, {
      :enable_starttls_auto => false,
      :address => params[:host] || params["host"] || @host,
      :port => params[:port] || params["port"] || @port.to_i,
    }
  mail.deliver
end

#write(data) ⇒ Object



21
22
23
# File 'lib/log4r/mail_outputter.rb', line 21

def write(data)
  send_mail(data.is_a?(Hash) ? data : {body: data})
end