Skip to content

Commit d603b66

Browse files
marcomarinhoclairton
authored andcommitted
feat: Add attachments in email reply
1 parent d5994bb commit d603b66

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

app/mailers/conversation_reply_mailer_helper.rb

+27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@ def prepare_mail(cc_bcc_enabled)
1919

2020
Rails.logger.info("Email sent from #{email_from} to #{to_emails} with subject #{mail_subject}")
2121

22+
if @message.attachments.present?
23+
@options[:attachments] = []
24+
25+
@message.attachments.each do |attachment|
26+
raw_data = attachment.file.download
27+
attachment_name = attachment.file.filename.to_s
28+
temp_dir = Rails.root.join('tmp/uploads')
29+
FileUtils.mkdir_p(temp_dir)
30+
temp_file_path = File.join(temp_dir, attachment_name)
31+
File.write(temp_file_path, raw_data, mode: 'wb')
32+
temp_file_path
33+
34+
# Get the size of the file before downloading
35+
file_size = raw_data.bytesize
36+
37+
if file_size < 25.megabytes
38+
# Store the temp file path and attachment name
39+
@options[:attachments] << { name: attachment_name, path: temp_file_path }
40+
mail.attachments[attachment_name] = File.read(temp_file_path)
41+
42+
Rails.logger.info("Attachment saved to #{temp_file_path}.")
43+
else
44+
Rails.logger.warn("Attachment #{attachment_name} is larger than 25MB and will be sent as a link")
45+
end
46+
end
47+
end
48+
2249
mail(@options)
2350
end
2451

app/views/mailers/conversation_reply_mailer/conversation_transcript.html.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
<% end %>
3535
<% if message.attachments %>
3636
<% message.attachments.each do |attachment| %>
37-
Attachment [<a href="<%= attachment.file_url %>" _target="blank">Click here to view</a>]
37+
Attachment
38+
<p>[<a href="<%= attachment.file_url %>" _target="blank"><%= attachment.file.filename.to_s %></a>]</p>
3839
<% end %>
3940
<% end %>
4041
<p style="font-size: 90%; font-size: 90%;color: #899096;margin-top: -8px; margin-bottom: 0px;">
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<% if @message.content %>
22
<%= ChatwootMarkdownRenderer.new(@message.content).render_message %>
33
<% end %>
4-
<% if @message.attachments %>
4+
<% if @message.attachments && !defined?(@options[:attachments]) %>
55
<% @message.attachments.each do |attachment| %>
6-
attachment [<a href="<%= attachment.file_url %>" _target="blank">click here to view</a>]
6+
<p>attachment</p>
7+
<p>[<a href="<%= attachment.file_url %>" _target="blank"><%= attachment.file.filename.to_s %></a>]</p>
78
<% end %>
89
<% end %>

0 commit comments

Comments
 (0)