Send .zip file as an Email Attachment in Rails - ruby-on-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.

Related

How to send the zip file in Rails via Grape API

I have a set of files that are present in s3 and I have to zip them all and send the zipped file to the front end(ReactJS).
I am successfully able to create a folder in the tmp of the project and also zip them. Unfortunately, I get the error when I try to expand saying Unable to expand
Here is the code -
data = Zip::File.open(zip_file_name, ::Zip::File::CREATE) do |zipfile|
files.each do |file|
zipfile.add(file, file_path)
end
end
content_type "application/octet-stream"
header['Content-Disposition'] = "attachment; filename=abcd.zip"
env['api.format'] = :binary
File.open(zip_file_name, 'rb').read
Is there a way to solve the problem? Thanks

Email attachment for document files using rails action mailer

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".

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/

FTP getbinaryfile by file extension?

I am using Net::FTP's getbinaryfile functionality to pull in a zip file with FTP. My system is not aware of the full file name, so I would simply like to search the folder for the zip file extension. Usually I would simply input the filename as *.zip. This does not seem to work.
ftp = Net::FTP.new(domain)
path = "#{Rails.root}/public/ftp/#{self.id}.zip"
ftp.getbinaryfile("*.zip", path)
I used the following code to return the zip file name in the FTP folder. Then using the same code as above, I was able to run getbinaryfile with the correct zip file name.
files = ftp.nlst("*.zip")
I use the following to get all zip files (I'm using SFTP but hopefully this will point you in the right direction)
Net::SFTP.start(domain, user, :password => 'pass') do |sftp|
sftp.dir.glob("/yourdirectory","*.zip").each do |file|
sftp.download!(file, "/local/spot")
end
end

Resources