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] =  File.read(document.public_filename)

      

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 => File.read(document.public_filename), :transfer_encoding => :binary }

      

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

+3


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] =  File.read(document.public_filename, :mode => 'rb')

      



or

attachments[document.display_name] =  File.read(document.public_filename, :encoding => 'BINARY')

      

+4


source







All Articles