在 Rails3 的 ActionMailer 中,我想发送一个 .txt 文件附件。挑战在于这个 txt 文件并不存在,我需要根据我拥有的大块文本内容创建这个 txt 文件。
可能吗?有什么想法吗?谢谢。
可能吗?有什么想法吗?谢谢。
在ActionMailer::Base的API文档中对文件进行了描述。
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['free_book.pdf'] = File.read('path/to/file.pdf')
mail(:to => recipient, :subject => "New account information")
end
end
但那并不一定要是一个文件,也可以是一个字符串。所以你可以做像这样的事情(我还使用了基于哈希的长格式,其中你也可以指定自己的MIME类型,你可以在ActionMailer::Base#attachments中找到有关此文档的说明):
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['filename.jpg'] = {:mime_type => 'application/mymimetype',
:content => some_string }
mail(:to => recipient, :subject => "New account information")
end
end
Mail::Message
对象,您可以向其中添加附件:ActionMailer::Base.mail(from: ..., to: ...).attachments['filename.jpg'] = some_string
。 - Joshua Pinter首先是发送电子邮件的方法
class ApplicationMailer < ActionMailer::Base
def welcome(user, filename, path)
attachments[filename] = File.read(path)
mail(:to => user.email, :subject => "New account information")
end
end
使用参数调用方法
UserMailer.welcome(user, filename, path).deliver
attachments
哈希。你可以将文件绑定到该哈希中,例如attachments['file.xlsx'] = File.read(/path/to/file.xlsx)
。 - undefined