Rails 3.0.7 ActionMailer attachment issue

穿精又带淫゛_ 提交于 2019-12-12 01:34:14

问题


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!


回答1:


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



回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/6105561/rails-3-0-7-actionmailer-attachment-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!