在我的Rails应用程序中,我试图将发票附加到电子邮件中:
def invoice(invoice) attachment :content_disposition => "attachment",:body => InvoicePdf.new(invoice),:content_type => "application/pdf",:filename => 'invoice.pdf' mail(:to => @user.email,:subject => "Your Invoice") end
InvoicePdf是一个对虾PDF文档:
class InvoicePdf < Prawn::Document def initialize(order,view) draw_pdf end def draw_pdf # pdf stuff end end
电子邮件中没有附件.我究竟做错了什么?任何提示将受到欢迎和赞赏.
编辑:我使用的Rails版本是3.0.x
解决方法
看看
Action Mailer Guide.你需要调用附件方法给你添加一个附件.
尝试这个:
attachments['attachment_filename'] = InvoicePdf.new(invoice)
这是假设调用InvoicePdf.new(invoice)生成一个文件并返回一个表示该文件的IO对象.我也注意到,您的InvoicePdf类初始化程序期望两个参数,但您只传递一个参数.
更新:
还要注意的是,Action Mailer将取得文件名并设计出mime类型,设置Content-Type,Content-Disposition,Content-Transfer-Encoding和base64编码附件内容全部为您手动设置必要的,除非你想覆盖默认值.
invoice = InvoicePdf.new(invoice) attachments["invoice.pdf"] = { :mime_type => 'application/pdf',:content => invoice.render } mail(:to => @user.email,:subject => "Your Invoice")