Skip to content

Commit

Permalink
feat: Add attachments in email reply
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomarinho authored and clairton committed Dec 18, 2024
1 parent d5994bb commit d603b66
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
27 changes: 27 additions & 0 deletions app/mailers/conversation_reply_mailer_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
<% end %>
<% if message.attachments %>
<% message.attachments.each do |attachment| %>
Attachment [<a href="<%= attachment.file_url %>" _target="blank">Click here to view</a>]
Attachment
<p>[<a href="<%= attachment.file_url %>" _target="blank"><%= attachment.file.filename.to_s %></a>]</p>
<% end %>
<% end %>
<p style="font-size: 90%; font-size: 90%;color: #899096;margin-top: -8px; margin-bottom: 0px;">
Expand Down
Original file line number Diff line number Diff line change
@@ -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 [<a href="<%= attachment.file_url %>" _target="blank">click here to view</a>]
<p>attachment</p>
<p>[<a href="<%= attachment.file_url %>" _target="blank"><%= attachment.file.filename.to_s %></a>]</p>
<% end %>
<% end %>

0 comments on commit d603b66

Please sign in to comment.