Forum: Ruby on Rails Rails 3.0.7 ActionMailer attachments

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.
Af520b18a7666a48d9f8ff7f040f9eb1?d=identicon&s=25 crunch (Guest)
on 2011-05-24 23:18
(Received via mailing list)
Hello,

I'm converting my 2.3.8 application to 3.0.7 and have run into a snag
with forwarding emails from the app with an attachment. The attachment
appears in the email as 1 byte in size and thus corrupt. The actual
file is fine and Rails seems to have access to the file. I've tried
multiple permutations but here's what I'm doing...

I'm fetching email from IMAP and storing it in the db. Attachments are
saved to the file system and the Attachment model stores the name of
the attachment and belongs to an Email. In the view there's a simple
form that allows an email address to forward the email to. That's all
working fine. It's just the attachment part that I'm having troubles
with.

def forward_email(email_id, from_address, to_address)
    @email = Email.find(email_id)
    @recipients = to_address
    @from = from_address
    @subject = @email.subject
    @sent_on = Time.now
    @body = @email.body + "\n\n"

    @email.attachments.each do |file|
      if File.exist?(file.full_path)
        attachment :filename => file.file_name,
                         :content => File.read(file.full_path),
                         :content_type => "application/octet-stream"
      else
        @body += "ATTACHMENT NOT FOUND: #{file.file_name}\n\n"
      end
    end
end

If I try the attachment part like this...

attachments[file.file_name] = File.read(file.full_path)

...then I get a text representation of the attachment at the bottom of
the email. I've also tried changing many different settings for
content_type but it doesn't seem to make a difference.

Any help would be much appreciated!

Thanks,

Chris
This topic is locked and can not be replied to.