Forum: Ruby on Rails ActionMailer - Adding an attachment from S3

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4efd15a59825388ea3382ec4d482a7d8?d=identicon&s=25 CuriousNewbie (Guest)
on 2010-12-05 19:21
(Received via mailing list)
Seems simple enough but I haven't been able to get it to work. The
files work fine from S3 on my web app, but when I email them out via
ActionMailer as attachments, see the code below, the files are
corrupt.

App Stack: rails 3, heroku, paperclip + s3

Here's the code:

    class UserMailer < ActionMailer::Base
    # Add Attachments if any
    if @comment.attachments.count > 0
      @comment.attachments.each do |a|
        require 'open-uri'
        open("#{Rails.root.to_s}/tmp/#{a.attachment_file_name}", "wb")
do |file|
          file << open(a.authenticated_url()).read
          attachments[a.attachment_file_name] =
File.read("#{Rails.root.to_s}/tmp/#{a.attachment_file_name}")
        end
      end
    end

    mail( :to => "#{XXXX}",
          :reply_to => "XXXXX>",
          :subject => "XXXXXX"
          )


a.authenticated_url() just gives me a URL to s3 to get the file (of
any type), I checked this, works fine. Something to do with the way
I'm saving the tempfile must be breaking the ActionMailer Attachment.

Any ideas?
This topic is locked and can not be replied to.