From 2805cc72a21089ea9725352b1d4d39d7d929a7a0 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Thu, 2 May 2013 15:34:08 +0100 Subject: Ignore common TNEF attachment parsing errors This also introduces a custom error class so that we don't accidentally catch other problems. Fixes #920 --- lib/mail_handler/backends/mail_backend.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'lib/mail_handler/backends/mail_backend.rb') diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index f7893a60d..bd3a91569 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -141,9 +141,14 @@ module MailHandler end elsif get_content_type(part) == 'application/ms-tnef' # A set of attachments in a TNEF file - part.rfc822_attachment = mail_from_tnef(part.body.decoded) - if part.rfc822_attachment.nil? - # Attached mail didn't parse, so treat as binary + begin + part.rfc822_attachment = mail_from_tnef(part.body.decoded) + if part.rfc822_attachment.nil? + # Attached mail didn't parse, so treat as binary + part.content_type = 'application/octet-stream' + end + rescue TNEFParsingError + part.rfc822_attachment = nil part.content_type = 'application/octet-stream' end end -- cgit v1.2.3 From 2d5ddc2d37837cb9ee80931b09dff04cc1120bda Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Wed, 15 May 2013 14:52:24 +0100 Subject: Make efforts to ensure that we're usually dealing with UTF-8 strings One of these changes is to make sure that the Mail backend, like the TMail backend it replaces, will return text parts encoded in UTF-8 if possible. The other change is to ensure that when text attachments are reloaded from disk, we attempt to convert them to UTF-8. --- lib/mail_handler/backends/mail_backend.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/mail_handler/backends/mail_backend.rb') diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index bd3a91569..f0dfc843c 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -38,7 +38,11 @@ module MailHandler # Get the body of a mail part def get_part_body(part) - part.body.decoded + decoded = part.body.decoded + if part.content_type =~ /^text\// + decoded = convert_string_to_utf8_or_binary decoded, part.charset + end + decoded end # Return the first from field if any -- cgit v1.2.3 From e01774371ae7b4a5f9e3ef8ee0280453736bd4f2 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 16 May 2013 16:37:46 +0100 Subject: Only set original_charset when a charset has been defined for the mail part. Fixes #942. --- lib/mail_handler/backends/mail_backend.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'lib/mail_handler/backends/mail_backend.rb') diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index f0dfc843c..6c213d370 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -169,8 +169,11 @@ module MailHandler part.parts.each{ |sub_part| expand_and_normalize_parts(sub_part, parent_mail) } else part_filename = get_part_file_name(part) - charset = part.charset # save this, because overwriting content_type also resets charset - + if part.has_charset? + original_charset = part.charset # save this, because overwriting content_type also resets charset + else + original_charset = nil + end # Don't allow nil content_types if get_content_type(part).nil? part.content_type = 'application/octet-stream' @@ -189,7 +192,9 @@ module MailHandler # Use standard content types for Word documents etc. part.content_type = normalise_content_type(get_content_type(part)) decode_attached_part(part, parent_mail) - part.charset = charset + if original_charset + part.charset = original_charset + end end end -- cgit v1.2.3 From 46e7df935929793fafb6069fbd272f5a35752e89 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Fri, 17 May 2013 11:48:14 +0100 Subject: Cope with emails with a missing final MIME boundary The Mail gem deals with multipart messages that look as if they should have 1 part but are missing the final MIME boundary, by make the parts list empty and setting part.body to the text of the email. Rather than throwing an exception in this case, we just pretend that part is text/plain and return it, so that the page doesn't error and we still have a chance of some useful text being displayed. Note that we haven't investigated yet the case of emails that have more than one start boundary, but no final boundary. Fixes #921 --- lib/mail_handler/backends/mail_backend.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lib/mail_handler/backends/mail_backend.rb') diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index 6c213d370..a97e68138 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -242,8 +242,15 @@ module MailHandler def _get_attachment_leaves_recursive(part, within_rfc822_attachment, parent_mail) leaves_found = [] if part.multipart? - raise "no parts on multipart mail" if part.parts.size == 0 - if part.sub_type == 'alternative' + if part.parts.size == 0 + # This is typically caused by a missing final + # MIME boundary, in which case the text of the + # message (including the opening MIME + # boundary) is in part.body, so just add this + # part as a leaf and treat it as text/plain: + part.content_type = "text/plain" + leaves_found += [part] + elsif part.sub_type == 'alternative' best_part = choose_best_alternative(part) leaves_found += _get_attachment_leaves_recursive(best_part, within_rfc822_attachment, -- cgit v1.2.3 From 6e64eb8fd3a346c24990553f294fb9d1f0ae6bbc Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Tue, 21 May 2013 17:03:08 +0100 Subject: 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. --- lib/mail_handler/backends/mail_backend.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'lib/mail_handler/backends/mail_backend.rb') 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. ' Date: Wed, 22 May 2013 13:47:01 +0100 Subject: Move the mapi requires to where they're really needed Handling of outlook-packed attachments would fail from rake tasks or in the console without requiring 'mapi/msg' and 'mapi/convert' beforehand. Instead, require them in the source file where they're actually used. --- lib/mail_handler/backends/mail_backend.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/mail_handler/backends/mail_backend.rb') diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index 42180dd6f..03d78e0a3 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -1,4 +1,6 @@ require 'mail' +require 'mapi/msg' +require 'mapi/convert' module Mail class Message -- cgit v1.2.3