diff options
author | Louise Crow <louise.crow@gmail.com> | 2012-12-04 09:53:16 +0000 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2012-12-04 09:53:16 +0000 |
commit | 9dfe9de140242c176eef8af65d78ced3f2992cc1 (patch) | |
tree | a823ea107091fa496796b00f738fb92f2745844e | |
parent | 30dd01f931ae3dea9a56812387bfea2544818e31 (diff) |
Move method for getting the to, cc and envelope-to addresses of a mail to the mail handler.
-rw-r--r-- | app/models/incoming_message.rb | 4 | ||||
-rw-r--r-- | lib/mail_handler/backends/mail_backend.rb | 7 | ||||
-rw-r--r-- | lib/mail_handler/backends/tmail_backend.rb | 6 | ||||
-rw-r--r-- | spec/fixtures/files/humberside-police-odd-mime-type.email | 25 | ||||
-rw-r--r-- | spec/lib/mail_handler/mail_handler_spec.rb | 29 |
5 files changed, 56 insertions, 15 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb index d6ced122b..088e6d0b0 100644 --- a/app/models/incoming_message.rb +++ b/app/models/incoming_message.rb @@ -83,9 +83,7 @@ class IncomingMessage < ActiveRecord::Base end def addresses - ((self.mail.to || []) + - (self.mail.cc || []) + - (self.mail.envelope_to || [])).uniq + MailHandler.get_all_addresses(self.mail) end def message_id diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index 4a8bd446c..2b1cef0d4 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -77,6 +77,13 @@ module MailHandler end end + def get_all_addresses(mail) + envelope_to = mail['envelope-to'] ? [mail['envelope-to'].value] : [] + ((mail.to || []) + + (mail.cc || []) + + (envelope_to || [])).uniq + end + # Format def address_from_name_and_email(name, email) if !MySociety::Validate.is_valid_email(email) diff --git a/lib/mail_handler/backends/tmail_backend.rb b/lib/mail_handler/backends/tmail_backend.rb index 445a81124..b8fdb2c88 100644 --- a/lib/mail_handler/backends/tmail_backend.rb +++ b/lib/mail_handler/backends/tmail_backend.rb @@ -59,6 +59,12 @@ module MailHandler mail.from_name_if_present end + def get_all_addresses(mail) + ((mail.to || []) + + (mail.cc || []) + + (mail.envelope_to || [])).uniq + end + def address_from_name_and_email(name, email) if !MySociety::Validate.is_valid_email(email) raise "invalid email " + email + " passed to address_from_name_and_email" diff --git a/spec/fixtures/files/humberside-police-odd-mime-type.email b/spec/fixtures/files/humberside-police-odd-mime-type.email index 5514b29da..ae4ceeffe 100644 --- a/spec/fixtures/files/humberside-police-odd-mime-type.email +++ b/spec/fixtures/files/humberside-police-odd-mime-type.email @@ -3,19 +3,20 @@ Return-path: <> Envelope-to: request-5335-xxxxxxxx@whatdotheyknow.com Delivery-date: Thu, 01 Jan 2009 15:56:20 +0000 Received: from earth.karoo.kcom.com ([212.50.160.55]:62894) - by sandwich.ukcod.org.uk with esmtp (Exim 4.63) - id 1LIPuG-0004AJ-B3 - for request-5335-xxxxxxxx@whatdotheyknow.com; Thu, 01 Jan 2009 15:56:20 +0000 + by sandwich.ukcod.org.uk with esmtp (Exim 4.63) + id 1LIPuG-0004AJ-B3 + for request-5335-xxxxxxxx@whatdotheyknow.com; Thu, 01 Jan 2009 15:56:20 +0000 Received: from unknown (HELO smtp-in.karoo.kcom.com) ([10.102.8.11]) by earth.karoo.kcom.com with ESMTP; 01 Jan 2009 15:44:42 +0000 Received: from exim by smtp-in.karoo.kcom.comwith local (Exim 4.30) - id 1LIPu0-0004fg-G6 server-id smtp-in4 - for request-5335-xxxxxxxx@whatdotheyknow.com; Thu, 01 Jan 2009 15:56:04 +0000 + id 1LIPu0-0004fg-G6 server-id smtp-in4 + for request-5335-xxxxxxxx@whatdotheyknow.com; Thu, 01 Jan 2009 15:56:04 +0000 X-Failed-Recipients: clerk@humberside-pa.karoo.co.uk Reply-To: Postmaster <postmaster@karoo.kcom.com> Auto-Submitted: auto-generated From: Mail Delivery System <Mailer-Daemon@karoo.co.uk> To: request-5335-xxxxxxxx@whatdotheyknow.com +Cc: request-5335-xxxxxxxx@whatdotheyknow.com Subject: Mail delivery failed : returning message to sender X-Mailer: Karoo Mailcore [version 2.0-IB] MIME-Version: 1.0 @@ -52,19 +53,19 @@ Content-Transfer-Encoding: 8bit Return-path: <request-5335-xxxxxxxx@whatdotheyknow.com> Received: from [212.50.160.60] (helo=venus.karoo.kcom.com) - by smtp-in.karoo.kcom.comwith esmtp (Exim 4.30) - id 1LIPu0-0004fc-FM server-id smtp-in4 - for clerk@humberside-pa.karoo.co.uk; Thu, 01 Jan 2009 15:56:04 +0000 + by smtp-in.karoo.kcom.comwith esmtp (Exim 4.30) + id 1LIPu0-0004fc-FM server-id smtp-in4 + for clerk@humberside-pa.karoo.co.uk; Thu, 01 Jan 2009 15:56:04 +0000 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnECAF9nXElSb+bUmWdsb2JhbACMZQGHFQEBAQEBCAsKBxG2eIVy -X-IronPort-AV: E=Sophos;i="4.36,313,1228089600"; +X-IronPort-AV: E=Sophos;i="4.36,313,1228089600"; d="scan'208";a="465483300" Received: from sandwich.ukcod.org.uk ([82.111.230.212]) by venus.karoo.kcom.com with ESMTP; 01 Jan 2009 15:46:44 +0000 Received: from foi by sandwich.ukcod.org.uk with local (Exim 4.63) - (envelope-from <request-5335-xxxxxxxx@whatdotheyknow.com>) - id 1LIPtz-0004AG-OC - for clerk@humberside-pa.karoo.co.uk; Thu, 01 Jan 2009 15:56:03 +0000 + (envelope-from <request-5335-xxxxxxxx@whatdotheyknow.com>) + id 1LIPtz-0004AG-OC + for clerk@humberside-pa.karoo.co.uk; Thu, 01 Jan 2009 15:56:03 +0000 From: John Jarman <request-5335-xxxxxxxx@whatdotheyknow.com> To: FOI requests at Humberside Police Authority <clerk@humberside-pa.karoo.co.uk> Subject: Freedom of Information request - Police Injury Award Pensions diff --git a/spec/lib/mail_handler/mail_handler_spec.rb b/spec/lib/mail_handler/mail_handler_spec.rb index 805fd6800..4603b695f 100644 --- a/spec/lib/mail_handler/mail_handler_spec.rb +++ b/spec/lib/mail_handler/mail_handler_spec.rb @@ -77,3 +77,32 @@ describe 'when asked for the from address' do MailHandler.get_from_address(mail).should == 'public@authority.gov.uk' end end + +describe 'when asked for all the addresses a mail has been sent to' do + + it 'should return an array containing the envelope-to address and the to address, and the cc address if there is one' do + mail_data = load_file_fixture('humberside-police-odd-mime-type.email') + mail_data.gsub!('Envelope-to: request-5335-xxxxxxxx@whatdotheyknow.com', + 'Envelope-to: request-5555-xxxxxxxx@whatdotheyknow.com') + mail_data.gsub!('Cc: request-5335-xxxxxxxx@whatdotheyknow.com', + 'Cc: request-3333-xxxxxxxx@whatdotheyknow.com') + mail = MailHandler.mail_from_raw_email(mail_data) + MailHandler.get_all_addresses(mail).should == ['request-5335-xxxxxxxx@whatdotheyknow.com', + 'request-3333-xxxxxxxx@whatdotheyknow.com', + 'request-5555-xxxxxxxx@whatdotheyknow.com'] + end + + it 'should only return unique values' do + # envelope-to and to fields are the same + mail = get_fixture_mail('humberside-police-odd-mime-type.email') + MailHandler.get_all_addresses(mail).should == ['request-5335-xxxxxxxx@whatdotheyknow.com'] + end + + it 'should handle the absence of an envelope-to or cc field' do + mail_data = load_file_fixture('autoresponse-header.email') + mail_data.gsub!('To: FOI Person <EMAIL_TO>', + 'To: FOI Person <request-5555-xxxxxxxx@whatdotheyknow.com>') + mail = MailHandler.mail_from_raw_email(mail_data) + MailHandler.get_all_addresses(mail).should == ["request-5555-xxxxxxxx@whatdotheyknow.com"] + end +end |