New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: handle multiple attachments file #76
Conversation
@BoutValentin thank you very much for that fix, I've installed mail_from using your repository and was able to send two files. Finally! <3 |
When setting up a form uploading multiple files through a single attachment, mail form was discarding all attachments and sending none, even though it could consider the form as delivered successfully. Now we will detect multiple files and add each of them individually. Note: the reason we are not checking for something like `respond_to?(:each)` and instead are doing a specific `is_a?(Array)` check, is because a single uploaded file from Rack (which we use in our tests) also responds to the `each` method (or rather, it delegates to the tempfile internal instance used for the upload). It appears that the Rails uploaded file does not do it, but for increased compatibility I decided to go with this implementation. The previous one worked because it tried both to add multiple files and handle a single file separately, so either one of them would work. This implementation makes an upfront decision about one or the other path, by performing the `is_a?` check. I don't love these types of checks, but this seemed the most straightforward way of doing it while keeping the code easier to follow. Closes #76
@BoutValentin thanks for your PR, and apologies for the long delay here. I cherry-picked it into a new branch #78 and tweaked it a bit, adding a test as well. It seems to be working fine. I'll be pushing it to master soon, if you or @czepesch are still able to give it a shot, feel free to try it then and let me know how it goes. |
When setting up a form uploading multiple files through a single attachment, mail form was discarding all attachments and sending none, even though it could consider the form as delivered successfully. Now we will detect multiple files and add each of them individually. Note: the reason we are not checking for something like `respond_to?(:each)` and instead are doing a specific `is_a?(Array)` check, is because a single uploaded file from Rack (which we use in our tests) also responds to the `each` method (or rather, it delegates to the tempfile internal instance used for the upload). It appears that the Rails uploaded file does not do it, but for increased compatibility I decided to go with this implementation. The previous one worked because it tried both to add multiple files and handle a single file separately, so either one of them would work. This implementation makes an upfront decision about one or the other path, by performing the `is_a?` check. I don't love these types of checks, but this seemed the most straightforward way of doing it while keeping the code easier to follow. Closes #76
@carlosantoniodasilva hello, Maybe I'm missing something. It works for you? |
@czepesch it worked when I originally implemented it 馃槃... are you using the latest version v1.10.0, or maybe the main branch directly? |
@carlosantoniodasilva main branch at first, and now checked with exactly "1.10.0" |
@czepesch that's pretty much it... here's the diff on a sample app of the two changes I had to make: diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb
index ff2cf7d..0246d0f 100644
--- a/app/controllers/contacts_controller.rb
+++ b/app/controllers/contacts_controller.rb
@@ -4,7 +4,7 @@ class ContactsController < ApplicationController
end
def create
- contact_params = params.require(:contact).permit(:name, :email, :message, :file)
+ contact_params = params.require(:contact).permit(:name, :email, :message, :file => [])
@contact = Contact.new(contact_params)
if @contact.deliver
diff --git a/app/views/contacts/new.html.erb b/app/views/contacts/new.html.erb
index 4d10a96..411742b 100644
--- a/app/views/contacts/new.html.erb
+++ b/app/views/contacts/new.html.erb
@@ -18,7 +18,7 @@
</div>
<div>
<%= f.label :file %><br/>
- <%= f.file_field :file %>
+ <%= f.file_field :file, multiple: true %>
</div>
<div> That sent both files I uploaded successfully:
|
hm... I just sent two files successfully too, but I don't remember changing anything, just ctl+Z to where it has been.. anyway. |
Hi everyone, In my small project, I have used the same model (namely : the Contact model) and form for two different purposes:
My turnaround was to :
The first branch of my if else does not yield an array, and therefore properly handles the 'require: true' test 2/ tweak the strong params to handle both potential outputs, as per below 3/ to be on the safe side I added 'validate: { presence: true }' in the model: I would be keen to get some ideas on how to force one attachment with 'multiple: true,'. |
Hi 馃憢,
I'm currently using mail_form in a small project. In my use case, I have to handle multiple attachments in email. When I use your current last release of the code, multiple attachments are not being send. If I use only on attachment, everything works fine.
As I investigate,I found out that when multiple attachments file are use, the send method respond with an array of elements
ActionDispatch::Http::UploadedFile
. To handle this use case I made a fork of your projects and make small change in the deliver file. It seems to work as expected in Rails 6.1.4.1 with Ruby 3.0.0.To be honest, I completely understand if this use case doesn't interested you and can stay with my current change in my own repo. I'm also a full beginner on the RoR frameworks and Ruby development and open to any discussion to improve the code I just propose to you.
Nice Day to everyone,
Thanks 鉂わ笍