Prawn pdf附件在电子邮件中

在我的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是一个Prawn 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编码附件的所有内容,以便手动设置isn除非你想覆盖默认值,否则是必要的。

根据您的pdf生成方法,这可能会更好:

 invoice = InvoicePdf.new(invoice) attachments["invoice.pdf"] = { :mime_type => 'application/pdf', :content => invoice.render } mail(:to => @user.email, :subject => "Your Invoice") 

不仅如此

 attachments["invoice.pdf"] = InvoicePdf.new(invoice) 

从3.0开始?