robinjohnobrien
robinjohnobrien

Reputation: 1092

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: [email protected], 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.

Upvotes: 2

Views: 2978

Answers (1)

robinjohnobrien
robinjohnobrien

Reputation: 1092

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.

Upvotes: 1

Related Questions