Project

General

Profile

Receiving Outlook inline Emails - cid: to !file.name!

Added by Matthew Paul about 7 years ago

I've installed the redmine_email_images plugin which is excellent! Now if I create or do an update to a ticket with inline images like -

!file.name! 

it also includes those images inline in outbound emails.

BUT for inbound emails from outlook where there there are inline images like screenshots, it attaches the screenshots ok but I still get like -

Some text

[cid:image001.png@01D29405.2CC43850]
A screenshot above and one below

[cid:image002.png@01D29405.2CC43850]

And end

So I think I just need to do a replace -

[cid:image002.png@01D29405.2CC43850]

with

!image002.png!


So, in the patch code below, how do I do that? Any ideas?
#encoding: utf-8

require_dependency 'mail_handler'

module MailHandlerRemoveInlineImagesPatch
  def self.included(base) # :nodoc:
    base.send(:include, InstanceMethods)
    base.class_eval do
      alias_method_chain :add_attachments, :remove_inline_images
    end
  end

  module InstanceMethods
    def add_attachments_with_remove_inline_images(obj)
      truncated = decoded_html.split(INVISIBLE_EMAIL_HEADER_DECODED, 2)[1]
      if truncated
        truncated.scan(FIND_IMG_SRC_PATTERN) do |_, src, _|
          src.match(/^.cid:(.+)./) do |m|
            remove_part_with_cid(email.parts, m[1])
          end
        end
      end
      add_attachments_without_remove_inline_images obj
    end

    def decoded_html
      return @decoded_html unless @decoded_html.nil?
      @decoded_html = decode_part_body(email.html_part)
    end

    private

    def remove_part_with_cid(parts, cid_to_remove)
      parts.select! do |part|
        keep = part.cid != cid_to_remove
        remove_part_with_cid(part.parts, cid_to_remove) if keep
        keep
      end
    end

    def decode_part_body(p)
      body_charset = Mail::RubyVer.respond_to?(:pick_encoding) ?
          Mail::RubyVer.pick_encoding(email.html_part.charset).to_s : p.charset
      Redmine::CodesetUtil.to_utf8(p.body.decoded, body_charset)
    end
  end
end

MailHandler.send(:include, MailHandlerRemoveInlineImagesPatch)


Replies (5)

RE: Receiving Outlook inline Emails - cid: to !file.name! - Added by Matthew Paul about 7 years ago

Ok, I've worked out what I want to do - like this -

"[cid:image002.png@01D29405.2CC43850]".gsub(/^\[cid:(.+)\@/, '!\1!').match('(.*)!')[1] + "!" 

returns this -

!image002.png!

So I think I want to execute that

gsub(/^\[cid:(.+)\@/, '!\1!').match('(.*)!')[1] + "!" 

function on the email body. But I have no idea how to do that. Because I'm an old programmer dude and Ruby blows my mind. So, any ideas? How would I fit that transformation gsub into what is returned there?

RE: Receiving Outlook inline Emails - cid: to !file.name! - Added by Andreas Schnöppl about 7 years ago

I have the same problem.
I wrote a regex but I have no idea where to place it.

I tried several Versions like

 p.body.decoded.gsub!(/\[cid:(\S+?)\@\S+?\]/, ' \1 ')
 @plain_text_body.sub! %r{\[cid:(\S+?)\@\S+?\]}, ' \1 '

but either nothing or partly strange things happened.
Now I am at the end of my ruby wisdom

RE: Receiving Outlook inline Emails - cid: to !file.name! - Added by Matthew Paul about 7 years ago

I think it goes in instance_methods but I don't know where. Maybe in the remove_part_with_cid routine?

See this post - #14945

RE: Receiving Outlook inline Emails - cid: to !file.name! - Added by Andreas Schnöppl over 6 years ago

Can anybody help?

I took the Code from #14945 and replaced the regex

module MailHandlerPatch
  def self.included(base)
    base.send(:include, InstanceMethods)
    base.class_eval do
      alias_method_chain :plain_text_body, :notes_cleanup
    end
  end

  module InstanceMethods
    def plain_text_body_with_notes_cleanup
      body = plain_text_body_without_notes_cleanup

      body.gsub!(/\[cid:(\S+?)\@\S+?\]/, ' \1 ')

      body
    end
  end
end

MailHandler.send(:include, MailHandlerPatch)

But what to do now? Save it in a file? Where? What Name? What to do else?

RE: Receiving Outlook inline Emails - cid: to !file.name! - Added by Andreas Schnöppl over 6 years ago

Works now for me

in plugins/mail_handler_patch/init.rb

require_dependency 'mail_handler_patch'

Redmine::Plugin.register :mail_handler_patch do

  name 'Mail Handler Patch'

end


in plugins/mail_handler_patch/lib/mail_handler_patch.rb

#encoding: utf-8

require_dependency 'mail_handler'

module MailHandlerPatch
  def self.included(base)
    base.send(:include, InstanceMethods)
    base.class_eval do
      alias_method_chain :plain_text_body, :notes_cleanup
    end
  end

  module InstanceMethods
    def plain_text_body_with_notes_cleanup
      body = plain_text_body_without_notes_cleanup
      body.gsub!(/\[cid:(\S+?)\@\S+?\]/, '[\1]')

      body
    end
  end
end

MailHandler.send(:include, MailHandlerPatch)


Then I restarted Apache and it worked.

Note: I did a Substitute to

[foo.jpg]
and not
!foo.jpg!

Because of a bug in my Redmine (2.5.5). Outlook tends to name all embedded screenshots image001.png. So if you write more than one mail to that issue you have duplicate image001.png.
The problem then is, that redmine replaces this image tags always with the last uploaded image001.png dynamically. So if you see the right image and somebody uploads another image001 then you see the wrong image.

    (1-5/5)