diff --git a/app/mailboxes/mailbox_helper.rb b/app/mailboxes/mailbox_helper.rb index fd812d7d4..f96266713 100644 --- a/app/mailboxes/mailbox_helper.rb +++ b/app/mailboxes/mailbox_helper.rb @@ -24,15 +24,55 @@ module MailboxHelper return if @message.blank? processed_mail.attachments.last(Message::NUMBER_OF_PERMITTED_ATTACHMENTS).each do |mail_attachment| - attachment = @message.attachments.new( - account_id: @conversation.account_id, - file_type: 'file' - ) - attachment.file.attach(mail_attachment[:blob]) + if inline_attachment?(mail_attachment) + embed_inline_image_source(mail_attachment) + else + attachment = @message.attachments.new( + account_id: @conversation.account_id, + file_type: 'file' + ) + attachment.file.attach(mail_attachment[:blob]) + end end @message.save! end + def embed_inline_image_source(mail_attachment) + current_mail = processed_mail.mail + + if current_mail.html_part.present? + upload_inline_image(mail_attachment) + elsif current_mail.text_part.present? + embed_plain_text_email_with_inline_image(mail_attachment) + end + end + + def upload_inline_image(mail_attachment) + content_id = mail_attachment[:original].cid + + @message.content_attributes[:email][:html_content][:full] = processed_mail.serialized_data[:html_content][:full].gsub( + "cid:#{content_id}", inline_image_url(mail_attachment[:blob]).to_s + ) + @message.save! + end + + def inline_attachment?(mail_attachment) + mail_attachment[:original].inline? + end + + def embed_plain_text_email_with_inline_image(mail_attachment) + attachment_name = mail_attachment[:original].filename + + @message.content_attributes[:email][:text_content][:full] = processed_mail.serialized_data[:text_content][:reply].gsub( + "[image: #{attachment_name}]", "\"#{attachment_name}\"" + ) + @message.save! + end + + def inline_image_url(blob) + Rails.application.routes.url_helpers.url_for(blob) + end + def create_contact @contact_inbox = ::ContactInboxWithContactBuilder.new( source_id: processed_mail.original_sender, diff --git a/app/presenters/mail_presenter.rb b/app/presenters/mail_presenter.rb index 92676b0d6..98915c07f 100644 --- a/app/presenters/mail_presenter.rb +++ b/app/presenters/mail_presenter.rb @@ -70,6 +70,8 @@ class MailPresenter < SimpleDelegator } end + # check content disposition check + # if inline, upload to AWS and and take the URL def attachments # ref : https://github.com/gorails-screencasts/action-mailbox-action-text/blob/master/app/mailboxes/posts_mailbox.rb mail.attachments.map do |attachment| diff --git a/spec/fixtures/files/forwarder_email.eml b/spec/fixtures/files/forwarder_email.eml index fb6d427f9..c75d062c7 100644 --- a/spec/fixtures/files/forwarder_email.eml +++ b/spec/fixtures/files/forwarder_email.eml @@ -25,7 +25,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -395,7 +395,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; diff --git a/spec/fixtures/files/group_sender_support.eml b/spec/fixtures/files/group_sender_support.eml index f13956104..9353718e0 100644 --- a/spec/fixtures/files/group_sender_support.eml +++ b/spec/fixtures/files/group_sender_support.eml @@ -26,7 +26,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -396,7 +396,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; diff --git a/spec/fixtures/files/in_reply_to.eml b/spec/fixtures/files/in_reply_to.eml index 3a565ea9e..084009929 100644 --- a/spec/fixtures/files/in_reply_to.eml +++ b/spec/fixtures/files/in_reply_to.eml @@ -24,7 +24,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -394,7 +394,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; diff --git a/spec/fixtures/files/mail_with_inline_images.eml b/spec/fixtures/files/mail_with_inline_images.eml new file mode 100644 index 000000000..2a7792ed1 --- /dev/null +++ b/spec/fixtures/files/mail_with_inline_images.eml @@ -0,0 +1,47 @@ +MIME-Version: 1.0 +Date: Wed, 31 May 2023 15:27:36 +0530 +Message-ID: +Subject: New inline image test email conversation +From: Sony Mathew +To: "Replies" +Content-Type: multipart/related; boundary="0000000000007a538405fcfa5967" + +--0000000000007a538405fcfa5967 +Content-Type: multipart/alternative; boundary="0000000000007a538205fcfa5966" + +--0000000000007a538205fcfa5966 +Content-Type: text/plain; charset="UTF-8" + +[image: ptrol.jpeg] + +HTML content and inline images + +-- +*Tejaswini Chile.* +Software developer +*Mob:8485827731 | *@tejaswini_chile + +--0000000000007a538205fcfa5966 +Content-Type: text/html; charset="UTF-8" +Content-Transfer-Encoding: quoted-printable + +
3D"ptrol.jpeg"

Let's add no HTML content here,= + just plain text and images

--
Tejaswini Chile.
Software developer
Mo= +b:8485827731 |=C2=A0@tejaswini_chile
+ +--0000000000007a538205fcfa5966-- +--0000000000007a538405fcfa5967 +Content-Type: image/jpeg; name="ptrol.jpeg" +Content-Disposition: inline; filename="ptrol.jpeg" +Content-Transfer-Encoding: base64 +X-Attachment-Id: ii_libj9tsr0 +Content-ID: + + +--0000000000007a538405fcfa5967-- diff --git a/spec/fixtures/files/mail_with_plain_text_and_inline_image.eml b/spec/fixtures/files/mail_with_plain_text_and_inline_image.eml new file mode 100644 index 000000000..b4ea01175 --- /dev/null +++ b/spec/fixtures/files/mail_with_plain_text_and_inline_image.eml @@ -0,0 +1,33 @@ +MIME-Version: 1.0 +Date: Wed, 31 May 2023 15:27:36 +0530 +Message-ID: +Subject: New inline image test email conversation +From: Sony Mathew +To: "Replies" +Content-Type: multipart/related; boundary="0000000000007a538405fcfa5967" + +--0000000000007a538405fcfa5967 +Content-Type: multipart/alternative; boundary="0000000000007a538205fcfa5966" + +--0000000000007a538205fcfa5966 +Content-Type: text/plain; charset="UTF-8" + +[image: ptrol.jpeg] + +Let's add no HTML content here, just plain text and images + +-- +*Tejaswini Chile.* +Software developer +*Mob:8485827731 | *@tejaswini_chile + + +--0000000000007a538405fcfa5967 +Content-Type: image/jpeg; name="ptrol.jpeg" +Content-Disposition: inline; filename="ptrol.jpeg" +Content-Transfer-Encoding: base64 +X-Attachment-Id: ii_libj9tsr0 +Content-ID: + + +--0000000000007a538405fcfa5967-- diff --git a/spec/fixtures/files/reply.eml b/spec/fixtures/files/reply.eml index 1ab026aca..13f92cc90 100644 --- a/spec/fixtures/files/reply.eml +++ b/spec/fixtures/files/reply.eml @@ -25,7 +25,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -395,7 +395,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; diff --git a/spec/fixtures/files/reply_mail_without_uuid.eml b/spec/fixtures/files/reply_mail_without_uuid.eml index c5ab3187b..790979734 100644 --- a/spec/fixtures/files/reply_mail_without_uuid.eml +++ b/spec/fixtures/files/reply_mail_without_uuid.eml @@ -25,7 +25,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -395,7 +395,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; diff --git a/spec/fixtures/files/reply_to.eml b/spec/fixtures/files/reply_to.eml index 03ab4f87c..eae762d78 100644 --- a/spec/fixtures/files/reply_to.eml +++ b/spec/fixtures/files/reply_to.eml @@ -26,7 +26,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -396,7 +396,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; diff --git a/spec/fixtures/files/support.eml b/spec/fixtures/files/support.eml index 5eee2ea12..68ea7d00b 100644 --- a/spec/fixtures/files/support.eml +++ b/spec/fixtures/files/support.eml @@ -25,7 +25,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -395,7 +395,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; @@ -628,4 +628,4 @@ r/Fxp3Y0d2/4tvsR95f/AAH7Gvwmn9rUHsOgUCg//9k= --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1-- ---Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74-- \ No newline at end of file +--Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74-- diff --git a/spec/fixtures/files/support_in_reply_to.eml b/spec/fixtures/files/support_in_reply_to.eml index 7d90f37ee..3d005a957 100644 --- a/spec/fixtures/files/support_in_reply_to.eml +++ b/spec/fixtures/files/support_in_reply_to.eml @@ -26,7 +26,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -396,7 +396,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; diff --git a/spec/fixtures/files/support_uppercase.eml b/spec/fixtures/files/support_uppercase.eml index 9354312ee..01a02262f 100644 --- a/spec/fixtures/files/support_uppercase.eml +++ b/spec/fixtures/files/support_uppercase.eml @@ -26,7 +26,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -396,7 +396,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; @@ -629,4 +629,4 @@ r/Fxp3Y0d2/4tvsR95f/AAH7Gvwmn9rUHsOgUCg//9k= --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1-- ---Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74-- \ No newline at end of file +--Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74-- diff --git a/spec/fixtures/files/support_without_sender_name.eml b/spec/fixtures/files/support_without_sender_name.eml index c8d3f1829..a5da87454 100644 --- a/spec/fixtures/files/support_without_sender_name.eml +++ b/spec/fixtures/files/support_without_sender_name.eml @@ -25,7 +25,7 @@ Content-Type: multipart/related; --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar1.jpeg Content-Type: image/jpg; name="avatar1.jpeg" @@ -395,7 +395,7 @@ mk8VWW5WRGJGAOaAP//Z --Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1 Content-Transfer-Encoding: base64 -Content-Disposition: inline; +Content-Disposition: attachment; filename=avatar2.jpg Content-Type: image/jpg; x-unix-mode=0700; diff --git a/spec/mailboxes/reply_mailbox_spec.rb b/spec/mailboxes/reply_mailbox_spec.rb index 72fbd317d..d8d0991a8 100644 --- a/spec/mailboxes/reply_mailbox_spec.rb +++ b/spec/mailboxes/reply_mailbox_spec.rb @@ -62,7 +62,51 @@ RSpec.describe ReplyMailbox do it 'find channel with in-reply-to mail' do described_subject - expect(conversation_1.messages.last.content).to eq("Let's talk about these images:") + expect(conversation_1.messages.last.content).to include("Let's talk about these images:") + end + end + + context 'with inline attachments' do + let(:mail_with_inline_images) { create_inbound_email_from_fixture('mail_with_inline_images.eml') } + let(:described_subject) { described_class.receive mail_with_inline_images } + + before do + conversation.uuid = '6bdc3f4d-0bed-4515-a284-5d916fdde489' + conversation.save! + + described_subject + end + + it 'mail content contains img source' do + expect(conversation.messages.last.content).to include('HTML content and inline images') + end + + it 'will not add the attachments' do + expect(conversation.messages.last.attachments.count).to eq(0) + + html_full_content = conversation.messages.last.content_attributes[:email][:html_content][:full] + expect(html_full_content).to include('img') + end + end + + context 'with inline attachments and plain text' do + let(:mail_with_plain_text_and_inline_image) { create_inbound_email_from_fixture('mail_with_plain_text_and_inline_image.eml') } + let(:described_subject) { described_class.receive mail_with_plain_text_and_inline_image } + + before do + conversation.uuid = '6bdc3f4d-0bed-4515-a284-5d916fdde489' + conversation.save! + + described_subject + end + + it 'will not add the attachments' do + described_class.receive mail_with_plain_text_and_inline_image + text_full_content = conversation.messages.last.content_attributes[:email][:text_content][:full] + + expect(text_full_content).to include('img') + expect(conversation.messages.last.content).to include("Let's add no HTML content here, just plain text and images") + expect(conversation.messages.last.attachments.count).to eq(0) end end