From d603b662156f4461669298d2c913b62ce24258bc Mon Sep 17 00:00:00 2001 From: Marco Marinho Date: Wed, 19 Jun 2024 09:39:19 +0100 Subject: [PATCH] feat: Add attachments in email reply --- .../conversation_reply_mailer_helper.rb | 27 +++++++++++++++++++ .../conversation_transcript.html.erb | 3 ++- .../email_reply.html.erb | 5 ++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/mailers/conversation_reply_mailer_helper.rb b/app/mailers/conversation_reply_mailer_helper.rb index 198e9f2a19f32..b84863e3bd54d 100644 --- a/app/mailers/conversation_reply_mailer_helper.rb +++ b/app/mailers/conversation_reply_mailer_helper.rb @@ -19,6 +19,33 @@ def prepare_mail(cc_bcc_enabled) Rails.logger.info("Email sent from #{email_from} to #{to_emails} with subject #{mail_subject}") + if @message.attachments.present? + @options[:attachments] = [] + + @message.attachments.each do |attachment| + raw_data = attachment.file.download + attachment_name = attachment.file.filename.to_s + temp_dir = Rails.root.join('tmp/uploads') + FileUtils.mkdir_p(temp_dir) + temp_file_path = File.join(temp_dir, attachment_name) + File.write(temp_file_path, raw_data, mode: 'wb') + temp_file_path + + # Get the size of the file before downloading + file_size = raw_data.bytesize + + if file_size < 25.megabytes + # Store the temp file path and attachment name + @options[:attachments] << { name: attachment_name, path: temp_file_path } + mail.attachments[attachment_name] = File.read(temp_file_path) + + Rails.logger.info("Attachment saved to #{temp_file_path}.") + else + Rails.logger.warn("Attachment #{attachment_name} is larger than 25MB and will be sent as a link") + end + end + end + mail(@options) end diff --git a/app/views/mailers/conversation_reply_mailer/conversation_transcript.html.erb b/app/views/mailers/conversation_reply_mailer/conversation_transcript.html.erb index 0ea0f301b17d8..2f8158c0bd55e 100644 --- a/app/views/mailers/conversation_reply_mailer/conversation_transcript.html.erb +++ b/app/views/mailers/conversation_reply_mailer/conversation_transcript.html.erb @@ -34,7 +34,8 @@ <% end %> <% if message.attachments %> <% message.attachments.each do |attachment| %> - Attachment [Click here to view] + Attachment +

[<%= attachment.file.filename.to_s %>]

<% end %> <% end %>

diff --git a/app/views/mailers/conversation_reply_mailer/email_reply.html.erb b/app/views/mailers/conversation_reply_mailer/email_reply.html.erb index 1d943c27e7571..4459e886f0188 100644 --- a/app/views/mailers/conversation_reply_mailer/email_reply.html.erb +++ b/app/views/mailers/conversation_reply_mailer/email_reply.html.erb @@ -1,8 +1,9 @@ <% if @message.content %> <%= ChatwootMarkdownRenderer.new(@message.content).render_message %> <% end %> -<% if @message.attachments %> +<% if @message.attachments && !defined?(@options[:attachments]) %> <% @message.attachments.each do |attachment| %> - attachment [click here to view] +

attachment

+

[<%= attachment.file.filename.to_s %>]

<% end %> <% end %>