Ruby on rails 3 电子邮件中的文件名无效(ActionMailer)

Ruby on rails 3 电子邮件中的文件名无效(ActionMailer),ruby-on-rails-3,actionmailer,Ruby On Rails 3,Actionmailer,我在使用ActionMailer发送带有附件的电子邮件时遇到问题 问题是,当我在gmail中阅读邮件时,我的附件有“noname”文件名 通知函数 class Notifier < ActionMailer::Base def send_message attachments["text.txt"] = {:content => "hello"} mail(:to => "me@gmail.com", :subject => 'test') end

我在使用ActionMailer发送带有附件的电子邮件时遇到问题

问题是,当我在gmail中阅读邮件时,我的附件有“noname”文件名

通知函数

class Notifier < ActionMailer::Base
  def send_message
    attachments["text.txt"] = {:content => "hello"}
    mail(:to => "me@gmail.com", :subject => 'test')
  end
end
类通知程序“hello”} 邮件(:收件人=>)me@gmail.com“,:主题=>‘测试’) 结束 结束 消息头:

Date: Sun, 19 Dec 2010 23:18:00 +0100 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; filename=text.txt Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=text.txt Content-ID: ... 日期:2010年12月19日星期日23:18:00+0100 Mime版本:1.0 内容类型:文本/纯文本; 字符集=UTF-8; filename=text.txt 内容传输编码:7bit 内容处置:附件; filename=text.txt 内容ID:。。。 如何发送具有正确文件名的邮件


谢谢

请确保您有自己的观点

app/views/[class\u name]/[method\u name]


创建
app/views/notifier/send_message.erb
文件和
app/views/notifier/send_message.html.erb
文件。

Drew是正确的,如果缺少特定的邮件程序视图,则会出现此问题

恕我直言,gmail无法获取邮件编码并重命名附件

更多信息请参见Gmail帮助:


我认为,如果您没有为电子邮件定义正文,则部分设置不正确,最终会赢得一个包含所有附件的部分标题的大型“noname”文件

使用此邮件程序代码:

class Mailer < ActionMailer::Base
  def generic(args)
    args.reverse_merge! to: 'e@mail.com', from: 'e@mail.com'
    add_attachments! args.delete(:attachments)
    mail(args)
  end

  protected
  def add_attachments!(*files)
    files.flatten.compact.each do |file|
      attachments[File.basename(file)] = File.read(file)
    end
  end
end
当我执行此操作时,我会得到两个名称正确的单独文件:

Mailer.generic(attachments: [File.open('/path/to/file1.txt'), File.open('/path/to/file2.csv')]).deliver
Mailer.generic(body: '', attachments: [File.open('/path/to/file1.txt'), File.open('/path/to/file2.csv')]).deliver

实际上,您可以将视图命名为
send_message.text.erb
,以使其更清晰。为什么有两个单独的文件执行相同的操作?