class LogStash::Outputs::Email

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

Public Instance Methods

receive(event) click to toggle source
# File lib/logstash/outputs/email.rb, line 133
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)
    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

    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
register() click to toggle source
# File lib/logstash/outputs/email.rb, line 100
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