undefined method filename= for action mailer attachment
When trying to execute
attachment开发者_如何学C report[1] do |a|
a.body = File.read(report[0])
a.filename = report[0].gsub(/.*\//,'')
end
I get the following error
undefined method `filename=' for #<Mail::Part:0x929b3b0>
report[1] = application/vnd.ms-excel report[0] = path to my file
Trying another way with a simple text-file fails also
@file_name = 'test.txt'
create_attachment
attachment :content_type => "text/plain",
:filename => "Some useless attachment",
:body => File.read(@file_name)
create_attachment just creates the test.txt file. In this case the mail is sent with the attachment but the sent txt-file is empty.
Have you tried this?
attachments[report[0].gsub(/.*\//,'')] = File.read(report[0])
Have you tried:
attachments[report[0].gsub(/.*\//,'')] = {
:mime_type => 'text/plain',
:content => File.read(report[0]),
:content_disposition => 'attachment'
}
This may be helpfull. And your test.txt file should be in public folder.
attachment "text/plain" do |a|
a.filename = "test.txt"
a.body = File.read(RAILS_ROOT + "/public/test.txt")
end
and for a excel file
attachment "application/vnd-ms-excel" do |a|
a.filename = "test.xls"
a.body = File.read(RAILS_ROOT + "/public/test.xls")
end
Try this
mail.attachments[report[0].split('/').last] = File.read(report[0])
精彩评论