I have an app that parses incoming email with the Thoughtbot Griddler gem. Currently, we don't support saving attachments, so we send users a "warning email" letting them know that we don't yet support saving attachments.

The old version of my app (written in C++) was able to distinguish between "uploaded" attachments and "inline"(?) attachments like email signatures with pictures; however, I'm not sure how to do this with Griddler.

Someone suggested that the Content-Disposition property in @headers might offer a way to distinguish the two; however, from the output below, both "inline"(?) attachments (email signatures with pictures in this case) and a regularly attached file (braces.rb) have Content-Disposition: form-data.

I'm stuck! Below are 3 attachments accessed via @email.attachments from Griddler. The first two attachments are email signatures with images and the last one is just a text file (braces.rb). Thanks in advance for any advice or suggestions!

<ActionDispatch::Http::UploadedFile:0x007fe1c4207cd8 @tempfile=#<Tempfile:/tmp/RackMultipart20150131-27166-163vzl6>, @original_filename="C9FEA3DC-2D79-495C-84DD-F710F0FE9473_15_.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"attachment1\"; filename=\"C9FEA3DC-2D79-495C-84DD-F710F0FE9473_15_.png\"\r\nContent-Type: image/png\r\n">
<ActionDispatch::Http::UploadedFile:0x007fe1c4200190 @tempfile=#<Tempfile:/tmp/RackMultipart20150131-27166-1y1km7h>, @original_filename="C9FEA3DC-2D79-495C-84DD-F710F0FE9473_13_.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"attachment2\"; filename=\"C9FEA3DC-2D79-495C-84DD-F710F0FE9473_13_.png\"\r\nContent-Type: image/png\r\n"> 
<ActionDispatch::Http::UploadedFile:0x007fe1c4207ad0 @tempfile=#<Tempfile:/tmp/RackMultipart20150131-27166-1urxy75>, @original_filename="braces.rb", @content_type="text/plain", @headers="Content-Disposition: form-data; name=\"attachment3\"; filename=\"braces.rb\"\r\nContent-Type: text/plain\r\n">] 
1

There are 1 best solutions below

0
On

I had similar problem with mailgun adapter and I had to modify this adapter to add support for inline images. Mailgun removes content-id from headers and creates special hash mapping names to content-ids. If you use mailgun service you can try my modifications from https://github.com/pejuko/griddler-mailgun. Just modify your gemfile so it contains:

gem 'griddler-mailgun', github: 'pejuko/griddler-mailgun'

And then you can use someting like:

if attachment.headers =~ /name="([^"]+)"/
  content_id = @email.content_id_map.invert[$1]
  if content_id.present? && content_id =~ /<([^>]+)>/
    content_id = $1
  end
end