diff --git a/lib/logstash/inputs/imap.rb b/lib/logstash/inputs/imap.rb index 6ff34e7..698ccdf 100644 --- a/lib/logstash/inputs/imap.rb +++ b/lib/logstash/inputs/imap.rb @@ -225,7 +225,7 @@ def parse_mail(mail) @codec.decode(message) do |event| # Use the 'Date' field as the timestamp - event.timestamp = LogStash::Timestamp.new(mail.date.to_time) + event.timestamp = LogStash::Timestamp.new(mail.date&.to_time) process_headers(mail, event) if @headers_target diff --git a/spec/inputs/imap_spec.rb b/spec/inputs/imap_spec.rb index 0d9c74b..fa74908 100644 --- a/spec/inputs/imap_spec.rb +++ b/spec/inputs/imap_spec.rb @@ -170,6 +170,18 @@ end end + context "missing date" do + before do + mail.date = nil + end + + it "should not cause an exception" do + event = input.parse_mail(mail) + expect( event.include?('Date') ).to be false + expect( event.timestamp ).not_to be nil + end + end + context "headers_target => ''" do let(:config) { super().merge("headers_target" => '') }