Class: LogStash::Outputs::Email

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/email.rb

Overview

Send email when an output is received. Alternatively, you may include or exclude the email output execution using conditionals.

Instance Method Summary collapse

Instance Method Details

#receive(event) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/logstash/outputs/email.rb', line 125

def receive(event)


    @logger.debug? and @logger.debug("Creating mail with these settings : ", :via => @via, :options => @options, :from => @from, :to => @to, :cc => @cc, :subject => @subject, :body => @body, :content_type => @contenttype, :htmlbody => @htmlbody, :attachments => @attachments, :to => to, :to => to)
    formatedSubject = event.sprintf(@subject)
    formattedBody = event.sprintf(@body)
    formattedHtmlBody = event.sprintf(@htmlbody)
    mail = Mail.new
    mail.from = event.sprintf(@from)
    mail.to = event.sprintf(@to)
    if @replyto
      mail.reply_to = event.sprintf(@replyto)
    end
    mail.cc = event.sprintf(@cc)
    mail.subject = formatedSubject
    if @htmlbody.empty?
      formattedBody.gsub!(/\\n/, "\n") # Take new line in the email
      mail.body = formattedBody
    else
      mail.text_part = Mail::Part.new do
        content_type "text/plain; charset=UTF-8"
        formattedBody.gsub!(/\\n/, "\n") # Take new line in the email
        body formattedBody
      end
      mail.html_part = Mail::Part.new do
        content_type "text/html; charset=UTF-8"
        body formattedHtmlBody
      end
    end
    @attachments.each do |fileLocation|
      mail.add_file(fileLocation)
    end # end @attachments.each
    @logger.debug? and @logger.debug("Sending mail with these values : ", :from => mail.from, :to => mail.to, :cc => mail.cc, :subject => mail.subject)
    begin
      mail.deliver!
    rescue StandardError => e
      @logger.error("Something happen while delivering an email", :exception => e)
      @logger.debug? && @logger.debug("Processed event: ", :event => event)
    end
end

#registerObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/logstash/outputs/email.rb', line 94

def register
  require "mail"

  options = {
    :address              => @address,
    :port                 => @port,
    :domain               => @domain,
    :user_name            => @username,
    :password             => @password,
    :authentication       => @authentication,
    :enable_starttls_auto => @use_tls,
    :debug                => @debug
  }

  if @via == "smtp"
    Mail.defaults do
      delivery_method :smtp, options
    end
  elsif @via == 'sendmail'
    Mail.defaults do
      delivery_method :sendmail
    end
  else
    Mail.defaults do
      delivery_method :@via, options
    end
  end # @via tests
  @logger.debug("Email Output Registered!", :config => options, :via => @via)
end