Chris A.
Chris A.

Reputation: 303

Rails 3.0.7 ActionMailer attachment issue

I'm trying to attach a file to an outgoing email but the attachment size ends up being 1 byte. It doesn't matter what attachment I'm forwarding it always ends up in the email 1 byte in size (corrupt). Everything else looks ok to me.

The email information is pulled from an IMAP account and stored in the database for browsing purposes. Attachments are stored on the file system and it's file name stored as an associated record for the Email.

In the view there's an option to forward the email to another recipient. It worked in Rails 2.3.8 but for Rails 3 I've had to change the attachment part of the method so now it looks like...

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, :body => File.read(file.full_path)
      else
        @body += "ATTACHMENT NOT FOUND: #{file.file_name}\n\n"
      end
    end
end

I've also tried it with...

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

and adding :mime_type and :content_type to no avail.

Any help would be a appreciated.

Thanks!

Upvotes: 0

Views: 906

Answers (3)

user703099
user703099

Reputation: 15

This is what I tried and worked for me

 attachments.each do |file|
      attachment :content_type => MIME::Types.type_for(file.path).first.content_type, :body => File.read(file.path) 
 end

Upvotes: 1

Chris A.
Chris A.

Reputation: 303

Well, someone from the rails team answered my question. The problem lies with adding body content (@body) other than the attachment inside the method. If you're going to attach files you have to use a view template.

Upvotes: 0

Arsen7
Arsen7

Reputation: 12840

Is the file readable? Can you debug the issue by placing something like this?

logger.debug "File: #{file.full_path.inspect} : #{File.read(file.full_path).inspect[0..100]}"

Is there anything in your development.log?

Upvotes: 0

Related Questions