Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7861 (closed defect: fixed)

Opened 1 year ago

Last modified 10 months ago

[PATCH] [TINY] Attachments on emails received aren't decoded correctly

Reported by: orangechicken Assigned to: minam
Priority: normal Milestone: 1.2.4
Component: ActionMailer Version: 1.2.3
Severity: normal Keywords: attachment decoding
Cc:

Description

Maybe this is a regression, but TMail instances that have files (images in my case) as attachments aren't decoding them automatically when you access that part's #body method. That behavior goes against all of the tutorials I've seen online. They imply (or explicitly state) that accessing a part's body should decode it automatically.

I believe the workaround is to loop through all the parts and call base64_decode on them (if they're base64 encoded?) and then access. The wiki page on receiving email says at the very bottom (by Mike Mondragon) says that the attachments array is useless because the attachment isn't decoded by itself. ?

Attachments

base64_decoding.patch (3.3 kB) - added by orangechicken on 03/19/07 22:47:41.
raw_base64_decoded_string (8.4 kB) - added by orangechicken on 03/19/07 22:48:09.
Goes into actionmailer/test/fixtures/
raw_base64_encoded_string (11.2 kB) - added by orangechicken on 03/19/07 22:48:16.
Goes into actionmailer/test/fixtures/

Change History

03/19/07 08:29:08 changed by orangechicken

It's also entirely possible that I'm leaving something out. I do see the code that #body uses and might be the source of the "problem" (or not full understanding on my part). #body calls unquoted_body('utf-8') (utf-8 is the default for the to_charset argument). unquoted_body correctly sees that the content_transfer_encoding is 'base64' and converts the base64 quoted string to utf-8. Pretty sure that's not what I want.

Also, doesn't help the attachments array because all the attachments are converted to utf-8.

03/19/07 08:50:01 changed by orangechicken

OK, so I think I'm on to something:

# File lib/action_mailer/vendor/tmail/quoting.rb, line 80

def unquote_base64_and_convert_to(text, to, from)

convert_to(Base64.decode(text).first, to, from)

end

I believe the #first call should be removed because that causes only the first character to be returned as the body instead of the whole file. This would fix the attachments array too, I believe.

Guess I should submit a patch?

03/19/07 09:04:44 changed by orangechicken

Hallelujah! That fixes everything! Now.... about that patch...

03/19/07 09:44:01 changed by orangechicken

  • summary changed from Attachments on emails received aren't automatically decoded to [PATCH] Attachments on emails received aren't automatically decoded.

Here's the patch. I added an assertion to an existing test that checked the length of the PNG attachment. This patch also fixed one other failing quoting test in the suite.

03/19/07 09:46:48 changed by orangechicken

  • summary changed from [PATCH] Attachments on emails received aren't automatically decoded to [PATCH] [TINY] Attachments on emails received aren't automatically decoded.

03/19/07 09:52:56 changed by orangechicken

  • summary changed from [PATCH] [TINY] Attachments on emails received aren't automatically decoded to [PATCH] [TINY] Attachments on emails received aren't decoded correctly.

03/19/07 12:45:08 changed by bitsweat

  • keywords changed from actionmailer attachments to attachment decoding.
  • owner changed from core to minam.
  • component changed from ActiveRecord to ActionMailer.
  • milestone changed from 1.x to 1.2.4.

03/19/07 21:47:43 changed by orangechicken

I think there's more going on here. This patch doesn't work on my production server (CentOS) but does on my Ubuntu test server. I think the problem may lie in TMail::Base64. It tries to require 'tmail/base64.so' - which raises a LoadError on my (failing) production machine.

So I think what might need to happen, instead of my submitted patch, is that the output from c_decode needs to match rb_decode (and I imagine it'd be easier to make rb_decode match c_decode).

I'll dig deeper.

03/19/07 22:00:40 changed by orangechicken

Yeah I think that's the root of the problem. c_decode returns a String and rb_decode returns an Array.

03/19/07 22:43:51 changed by orangechicken

Added several more tests and fixed the actual problem (rb_decode output didn't match c_decode output).

03/19/07 22:47:41 changed by orangechicken

  • attachment base64_decoding.patch added.

03/19/07 22:48:09 changed by orangechicken

  • attachment raw_base64_decoded_string added.

Goes into actionmailer/test/fixtures/

03/19/07 22:48:16 changed by orangechicken

  • attachment raw_base64_encoded_string added.

Goes into actionmailer/test/fixtures/

03/19/07 22:49:32 changed by orangechicken

Make sure you grab the two raw_base64_* files attached and put them in actionmailer/test/fixtures. Not sure the patch included the decoded one since it's binary data.

05/24/07 21:27:07 changed by caio

09/14/07 05:08:14 changed by iamwil

This particular change saved my butt. +1 for this change. I'm not sure why it hasn't been added to edge rails yet...

09/14/07 05:18:33 changed by caio

where by 2241 i meant #2441

09/14/07 05:30:57 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [7476]) Fix attachment decoding when using the TMail C extension. Closes #7861.