Skip to content

Support adding custom headers #63

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 4.2.0
- Support adding custom headers [#57](https://github.com/logstash-plugins/logstash-output-email/pull/57)
- Update 'mail' dependency to ~> 2 and 'mustache' to ~> 1
- Remove constraint on 'mime-types'

## 4.1.1
- Docs: Set the default_codec doc attribute.

Expand Down
11 changes: 10 additions & 1 deletion docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
| <<plugins-{type}s-{plugin}-debug>> |<<boolean,boolean>>|No
| <<plugins-{type}s-{plugin}-domain>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-from>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-custom_headers>> |<<hash,hash>>|No
| <<plugins-{type}s-{plugin}-htmlbody>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-password>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-port>> |<<number,number>>|No
Expand Down Expand Up @@ -165,6 +166,14 @@ actual hostname of the connecting client.

The fully-qualified email address for the From: field in the email.

[id="plugins-{type}s-{plugin}-custom_headers"]
===== `custom_headers`

* Value type is <<hash,hash>>
* Default value is `{}`

Custom headers to attach to the email.

[id="plugins-{type}s-{plugin}-htmlbody"]
===== `htmlbody`

Expand Down Expand Up @@ -256,4 +265,4 @@ Can be used with `body` to send multi-part emails. Takes precedence over `htmlBo
[id="plugins-{type}s-{plugin}-common-options"]
include::{include_path}/{type}.asciidoc[]

:default_codec!:
:default_codec!:
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
9 changes: 3 additions & 6 deletions logstash-output-email.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|

s.name = 'logstash-output-email'
s.version = '4.1.1'
s.version = '4.2.0'
s.licenses = ['Apache License (2.0)']
s.summary = "Sends email to a specified address when output is received"
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
Expand All @@ -22,11 +22,8 @@ Gem::Specification.new do |s|
# Gem dependencies
s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"

s.add_runtime_dependency 'mail', '~> 2.6.3'
# mime-types >= 3 require ruby 2.0 support
s.add_runtime_dependency 'mime-types', '< 3'

s.add_runtime_dependency 'mustache', '>= 0.99.8'
s.add_runtime_dependency 'mail', '~> 2'
s.add_runtime_dependency 'mustache', '~> 1'

s.add_development_dependency 'logstash-devutils'
s.add_development_dependency 'rumbster'
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