aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMark Longair <mhl@pobox.com>2013-05-21 17:03:08 +0100
committerMark Longair <mhl@pobox.com>2013-05-21 17:10:21 +0100
commit6e64eb8fd3a346c24990553f294fb9d1f0ae6bbc (patch)
treeddc0928bdf57b098c4bd7d60955a10f944fca92e /lib
parent46e7df935929793fafb6069fbd272f5a35752e89 (diff)
Retain old handling of malformed addresses in To and Cc lines
The behaviour of the TMail backend's 'to' and 'cc' methods where there was a malformed To: or Cc: line was to return nil, whereas Mail returns a version of the string anyway. We'd have to change quite a lot of code to deal with an extra possible class of returned objects, so it's simplest for the moment to monkey-patch Mail::Message's 'to' and 'cc' methods to restore the old behaviour.
Diffstat (limited to 'lib')
-rw-r--r--lib/mail_handler/backends/mail_backend.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb
index a97e68138..42180dd6f 100644
--- a/lib/mail_handler/backends/mail_backend.rb
+++ b/lib/mail_handler/backends/mail_backend.rb
@@ -1,5 +1,34 @@
require 'mail'
+module Mail
+ class Message
+
+ # The behaviour of the 'to' and 'cc' methods have changed
+ # between TMail and Mail; this monkey-patching restores the
+ # TMail behaviour. The key difference is that when there's an
+ # invalid address, e.g. '<foo@example.org', Mail returns the
+ # string as an ActiveSupport::Multibyte::Chars, whereas
+ # previously TMail would return nil.
+
+ alias_method :old_to, :to
+ alias_method :old_cc, :cc
+
+ def clean_addresses(old_method, val)
+ old_result = self.send(old_method, val)
+ old_result.class == Mail::AddressContainer ? old_result : nil
+ end
+
+ def to(val = nil)
+ self.clean_addresses :old_to, val
+ end
+
+ def cc(val = nil)
+ self.clean_addresses :old_cc, val
+ end
+
+ end
+end
+
module MailHandler
module Backends
module MailBackend