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



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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/logstash/outputs/email.rb', line 136

def receive(event)


    @logger.debug? and @logger.debug("Creating mail with these settings : ", :via => @via, :options => @options, :from => @from, :to => @to, :cc => @cc, :bcc => @bcc, :subject => @subject, :body => @body, :content_type => @contenttype, :htmlbody => @htmlbody, :attachments => @attachments, :custom_headers => @custom_headers)
    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.bcc = event.sprintf(@bcc)
    mail.subject = formatedSubject

    @custom_headers.each do |key, value|
      mail.header[event.sprintf(key)] = event.sprintf(value)
    end

    if @htmlbody.empty? and @template_file.nil?
      formattedBody.gsub!(/\\n/, "\n") # Take new line in the email
      mail.body = formattedBody
    else
      # This handles multipart emails
      # cf: https://github.com/mikel/mail/#writing-and-sending-a-multipartalternative-html-and-text-email
      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
      
      if @template_file.nil?
        mail.html_part = Mail::Part.new do
          content_type "text/html; charset=UTF-8"
          body formattedHtmlBody
        end
      else
        templatedHtmlBody = Mustache.render(@htmlTemplate, event.to_hash)
        mail.html_part = Mail::Part.new do
          content_type "text/html; charset=UTF-8"
          body templatedHtmlBody
        end
      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, :bcc => mail.bcc, :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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/logstash/outputs/email.rb', line 103

def register
  require "mail"
  require "mustache"

  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
  @htmlTemplate = File.open(@template_file, "r").read unless @template_file.nil?
  @logger.debug("Email Output Registered!", :config => options, :via => @via)
end