mikel / mail

A Really Ruby Mail Library
MIT License
3.62k stars 936 forks source link

Handling outlook .msg attachments (rfc822) as attachments #1492

Open jcatrysse opened 2 years ago

jcatrysse commented 2 years ago

Hello,

This issue concerns Redmine 4.2.3, who is using this mail gem to handle incoming mails.

We have an issue when using forward mail as attachment from Outlook and sending the message to Redmine (who is using this mail gem). Normally, on reception, we would expect to receive the mail message and the added .msg attachment as an attachment. This is not the case.

We get the mail message, all the other attachments from the mail, but also from the added .msg attachment. But never the .msg attachment itself.

The .msg attachment is encoded like this:

...
--_012_342b0886c5894875a2dadf9b72a8b986TRUSTMBX01TRUSTTEAMLOCA_
Content-Type: message/rfc822
Content-Disposition: attachment;
    creation-date="Tue, 21 Jun 2022 16:05:12 GMT";
    modification-date="Tue, 21 Jun 2022 16:05:12 GMT"
...

And not like this:

--_012_9f65674b87e2415387c04fb36bdc7d93TRUSTMBX01TRUSTTEAMLOCA_
Content-Type: application/octet-stream; name="test_message .msg"
Content-Description: test_message .msg
Content-Disposition: attachment; filename="test_message.msg"; size=274579;
    creation-date="Tue, 21 Jun 2022 16:03:48 GMT";
    modification-date="Wed, 22 Jun 2022 13:27:38 GMT"
Content-Transfer-Encoding: base64

What can we do, just to handle rfc822 parts as an attachment?

All help is very much appreciated.

jcatrysse commented 2 years ago

I added some dirty code (I found on the internet) into Redmine to just add the rfc822 parts as attachments, but it would be nice to handle this correctly.

    logger.info "Checking multipart"
    if email.parts
      email.parts.each do |part|
        if (part.content_disposition =~  /attachment/i) && (part.content_type = "message/rfc822")
          logger.info  "Found email attachment in parts section"
          contents = part.decoded.gsub(0x0a.chr, 0x0d.chr+0x0a.chr) # this is to change newline type in attachment
          logger.info  "Trying to parse it as email"
          attachment_email = Mail.new(contents)
          obj.attachments << Attachment.create(:container => obj,
                                               :file => contents,
                                               :filename => attachment_email.subject + ".eml",
                                               :author => user,
                                               :content_type => part.content_type)
          logger.info  "Attachment added"
        end
      end
    end
    logger.info "Attachements added"
ahorek commented 2 years ago

looks similar to my 2 year old PR :) https://github.com/mikel/mail/pull/1389

jcatrysse commented 2 years ago

Yes, indeed... it does! Any idea why this hasn't been merged / implemented?

And maybe more important... can this be implemented in for example a 2.7.2 version? I'm not that familiar with this :-)