Ignore emails with Auto-Submitted: auto-replied header #190

pull/1186/head
Jean-Philippe Lang 12 years ago committed by Philipp Tessenow
parent 1a9fcd69ae
commit ff195605b3
  1. 17
      app/models/mail_handler.rb
  2. 17
      test/unit/mail_handler_test.rb

@ -40,6 +40,12 @@ class MailHandler < ActionMailer::Base
super email
end
cattr_accessor :ignored_emails_headers
@@ignored_emails_headers = {
'X-Auto-Response-Suppress' => 'oof',
'Auto-Submitted' => /^auto-/
}
# Processes incoming emails
# Returns the created object (eg. an issue, a message) or false
def receive(email)
@ -50,6 +56,17 @@ class MailHandler < ActionMailer::Base
logger.info "MailHandler: ignoring email from emission address [#{sender_email}]" if logger && logger.info
return false
end
# Ignore auto generated emails
self.class.ignored_emails_headers.each do |key, ignored_value|
value = email.header[key]
if value
value = value.to_s.downcase
if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value
logger.info "MailHandler: ignoring email with #{key}:#{value} header" if logger && logger.info
return false
end
end
end
@user = User.find_by_mail(sender_email) if sender_email.present?
if @user && !@user.active?
logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info

@ -265,6 +265,23 @@ class MailHandlerTest < ActiveSupport::TestCase
end
end
def test_should_ignore_auto_replied_emails
MailHandler.any_instance.expects(:dispatch).never
[
"X-Auto-Response-Suppress: OOF",
"Auto-Submitted: auto-replied",
"Auto-Submitted: Auto-Replied",
"Auto-Submitted: auto-generated"
].each do |header|
raw = IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
raw = header + "\n" + raw
assert_no_difference 'Issue.count' do
assert_equal false, MailHandler.receive(raw), "email with #{header} header was not ignored"
end
end
end
def test_add_issue_should_send_email_notification
Setting.notified_events = ['issue_added']
ActionMailer::Base.deliveries.clear

Loading…
Cancel
Save