Skip to content

Support custom headers #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/logstash/outputs/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class LogStash::Outputs::Email < LogStash::Outputs::Base
# NOTE: this may not be functional (KH)
config :contenttype, :validate => :string, :default => "text/html; charset=UTF-8"

# Custom headers to attach to the email
config :custom_headers, :validate => :hash, :default => {}

public
def register
require "mail"
Expand Down Expand Up @@ -133,7 +136,7 @@ def register
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)
@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)
Expand All @@ -148,6 +151,10 @@ def receive(event)
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
Expand Down
17 changes: 17 additions & 0 deletions spec/outputs/email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,22 @@
expect(message_observer.messages[0].body.decoded).to eq(craft_multi_part_email('', '<h1>hello</h1>', message_observer.messages[0].content_type))
end
end

context "custom headers for email" do
it "adds custom headers" do
headers = { "header_1" => "header_1_value", "header_2" => "header_2_value" }
subject = plugin.new("to" => "me@host",
"subject" => "Hello World",
"body" => "Line1\\nLine2\\nLine3",
"port" => port,
"custom_headers" => headers )
subject.register
subject.receive(LogStash::Event.new("message" => "hello"))
expect(message_observer.messages.size).to eq(1)
headers.each do |key, value|
expect(message_observer.messages[0].header[key].value).to eq(value)
end
end
end
end
end