Email attachment for document files using rails action mailer - ruby-on-rails

I am using Rails action mailer. I need to send a email with document(doc, docx, txt) as attachment. When I am attaching .txt files, the content in that txt file are viewing properly. But when I am trying to attach .doc or docx files, the content in that files are not displaying(it is converting into encoding format) Why is that happening. Is there any way to attach document files in email using rails action mailer.
def send_email(email, file)
#email = email
attachments['document.docx'] = File.read(file.path)
mail(from: #email, to: "aa#gmail.com", subject: "Document attachment")
end
original document attached
document received in email
Please guide me, if I am going wrong in some place.
Thanks in advance.

Try reading the file in binary mode (see binary file mode & :mode option:
attachments['document.docx'] = File.read(file.path, mode: 'rb')
In my case, Rails was adding a carriage return (aka "CR", or "\r") to every line in a CSV file that ended with either a "\n" (newline) or "\r".

Related

combine_pdf not combining the pdfs

I think I am missing something simple. Using combine_pdf: I am attempting to combine two pdf files into one pdf, and then send that resulting pdf with send_data in my rails app.
Here is my code in a controller:
pdf = CombinePDF.new
# returns an array, each element is a string of an absolute path
# to the file I want to upload
absolute_upload_paths = #obj.attachments.collect {|obj| obj.my_attachment.path}
absolute_upload_paths.each {|upload_path| pdf << CombinePDF.load(upload_path)}
send_data pdf, filename: “my_combined_pdf”, type: "application/pdf"
What results is that a damaged pdf file gets sent which cannot be opened:
Adobe Acrobat Reader could not open 'VR_Voc_Eval-51.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
What am I missing? How can I use this gem to combine two existing pdf files into one pdf and then send it to the user?
It looks like the README for that library calls .to_pdf when sending the data. Hopefully calling #to_pdf on the pdf object like in the example will fix your issue.
send_data pdf.to_pdf, filename: “my_combined_pdf”, type: "application/pdf"
https://github.com/boazsegev/combine_pdf#rendering-pdf-data

How to download files through a custom action in Rails Admin

How can I download a file through a custom action in Rails Admin. As the file which I'm sending is ZIP format but after getting downloaded its a nested zip which on opening creates another zip file and just goes on.
So basically this is my custom action where I'm calling my Service which will create a zip file through gem rubyzip and I'm sending the file_path of the created zip file with is on my local machine(e.g file_path as "#{Rails.root}/abc.zip")
else
# all your code that does the work
import_params = params.require(:documents)
.permit(:email)
if import_params[:email].present?
#result = Download::Forms.call(mail_id: import_params[:email])
if #result[:status]
flash.now[:notice] = 'File Downloaded'
send_file(
#result[:file_path],
filename: #result[:file_name],
type: "application/zip"
)
else
flash.now[:error] = "Failed to Download because #{#result[:error_message]}"
end
else
flash.now[:notice] = 'Please Enter Email Address'
end
#result
end
As I also send the #result in the end so that I can also try link_to method to download the file but all in vain. I'm using Rails 4.2.6
Some more information regarding the file types is that I tried a txt file and a pdf file also to check whether they are downloading properly but the same result they got downloaded but they were not proper as txt file was containing nothing and pdf can't be opened.
The files are getting downloaded with my custom action name and not from the file name which is the right naming if I download a file from any simple action.
Any help will be really appreciated.

Writing valid Excel file

I got an Endpoint that receives emails with .xlsx files as attachments. I want to save these file in my app, so I can later access the data.
After receiving the mail and its attachment - which has a mime_type of application/vnd.openxmlformats-officedocument.spreadsheetml.sheet- I call
path = "data/emails/#{attachment.filename}"
File.write(path, attachment.body.decoded)
but I get this error:
Encoding::UndefinedConversionError: "\x85" from ASCII-8BIT to UTF-8
When I use add .force_encoding('utf-8') to the decoded body, it does succeed, but the file it writes becomes invalid. I cannot open it normally, nor access its data.
How do I write a normal Excel file?
Does this work?
File.open( path, "w+b", 0644 ) { |f| f.write attachment.body.decoded }
Taken from here:
https://cbpowell.wordpress.com/2011/01/17/saving-attachments-with-ruby-1-9-2-rails-3-and-the-mail-gem/

Send .zip file as an Email Attachment in Rails

So I am trying to send an email containing a .zip file. The .zip file is located at another url on another server. I am able to retrieve the file, attach it and send it. However when I get the attachment from the email. It will not open as it says cannot open *.zip.zip I have tried removing the .zip in the name but then the archive manager cannot open it either.
Any ideas?
Code is below.
http = Net::HTTP.new('www.somedomaim.org')
http.start() { |http|
req = Net::HTTP::Get.new("/path/to/file.zip")
response = http.request(req)
tempfile = Tempfile.new('files')
File.open(tempfile.path, 'w') do |f|
f.write response.body
end
attachments["files.zip"] = File.read(tempfile.path)
mail to: someone#somewhere.com, subject: "Sending zip file"
}
[SOLVED]
The solution is rather simple.
attachments['files.zip'] = open('http://somedomain.com/path/to/file.zip').read
attachments needs to receive the content of the file. .read returns the content of the file. My issue was that i was placing the entire zip file in the content of a new file. The above solution will place just the content of the zip into a new file.
Hope this helps someone someday. Thanks for all the suggestions.
The solution is rather simple.
attachments['files.zip'] = open('http://somedomain.com/path/to/file.zip').read
attachments needs to receive the content of the file. .read returns the content of the file. My issue was that i was placing the entire zip file in the content of a new file. The above solution will place just the content of the zip into a new file.

Cannot send xlsx as attachment in Rails mailer

I am trying to send an email containing a file attachment created by the user.
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 upload the attachments, and delayed_job to defer sending the emails, however the file I am trying to attach looks ok, and I can open it successfully outside the application.
I also saw a suggestion to change the code as follows, but it does not seem to help:
include an extra.
attachments[document.display_name] = { :content => File.read(document.public_filename), :transfer_encoding => :binary }
How can I make the code work for all attachment types?
You need to specify the mode or encoding to let it read 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')

Resources