Ruby on rails 3 电子邮件中的文件名无效(ActionMailer)
我在使用ActionMailer发送带有附件的电子邮件时遇到问题 问题是,当我在gmail中阅读邮件时,我的附件有“noname”文件名 通知函数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
class Notifier < ActionMailer::Base
def send_message
attachments["text.txt"] = {:content => "hello"}
mail(:to => "me@gmail.com", :subject => 'test')
end
end
类通知程序谢谢请确保您有自己的观点 在
app/views/[class\u name]/[method\u name]
创建
app/views/notifier/send_message.erb
文件和app/views/notifier/send_message.html.erb
文件。Drew是正确的,如果缺少特定的邮件程序视图,则会出现此问题
恕我直言,gmail无法获取邮件编码并重命名附件
更多信息请参见Gmail帮助:
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
,以使其更清晰。为什么有两个单独的文件执行相同的操作?