Rails 3.0.7 ActionMailer附件问题 [英] Rails 3.0.7 ActionMailer attachment issue

查看:76
本文介绍了Rails 3.0.7 ActionMailer附件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件附加到外发电子邮件,但附件大小最终为1个字节。无论我转发什么附件,总是以1字节大小(损坏)的电子邮件结尾。其他一切对我来说都还不错。

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.

电子邮件信息是从IMAP帐户中提取的,并存储在数据库中以供浏览。附件存储在文件系统中,其文件名存储为电子邮件的关联记录。

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.

在视图中,可以选择将电子邮件转发给其他收件人。它可以在Rails 2.3.8中工作,但是对于Rails 3,我不得不更改该方法的附件部分,因此现在看起来像...

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

我也尝试过...

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

并添加:mime_type和:content_type无济于事。

and adding :mime_type and :content_type to no avail.

任何帮助将不胜感激。

谢谢!

推荐答案

好吧,来自Rails团队的人回答了我的问题。问题在于除了方法内部的附件之外,添加正文内容(@body)。如果要附加文件,则必须使用视图模板。

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.

这篇关于Rails 3.0.7 ActionMailer附件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆