Can't send xlsx as attachment to Rails mailer

I am trying to send an email with a user generated file attachment.

attachments[document.display_name] =


This works in most conditions (including .docx, but fails for .xlsx files with the error:

invalid byte sequence in UTF-8


I am using attachment_fu to download attachments and delayed_job to delay sending emails, however the file I am trying to attach looks fine and I can successfully open it outside of the application.

I've also seen a suggestion to change the code as follows, but it doesn't seem to help: include additional.

    attachments[document.display_name] =  { :content =>, :transfer_encoding => :binary }


How can I get the code to work for all types of attachments?


source to share

1 answer

You need to specify the mode or encoding so that it reads the file as binary:

attachments[document.display_name] =, :mode => 'rb')



attachments[document.display_name] =, :encoding => 'BINARY')




All Articles