Prawn pdf附件在电子邮件中

时间:2022-05-25 21:11:03

In my Rails application, I'm trying to attach the invoice to the email:

在我的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

The InvoicePdf is a Prawn PDF document:

InvoicePdf是一个Prawn PDF文档:

class InvoicePdf < Prawn::Document
  def initialize(order, view)
    draw_pdf
  end

  def draw_pdf
    # pdf stuff
  end
end

I get no attachments in the email. What am I doing wrong? Any tips would be welcomed and appreciated.

我在电子邮件中没有附件。我究竟做错了什么?任何提示都会受到欢迎和赞赏。

Edit: The Rails version I'm using is 3.0.x

编辑:我使用的Rails版本是3.0.x.

2 个解决方案

#1


14  

Take a look at the Action Mailer Guide. You need to call the attachments method for you to add an attachment.

看一下Action Mailer Guide。您需要调用附件方法来添加附件。

Try this:

尝试这个:

attachments['attachment_filename'] = InvoicePdf.new(invoice)

This is assuming that calling InvoicePdf.new(invoice) generates a file and returns an IO object representing that file. I also noticed that your InvoicePdf class initializer expects two parameters but you are passing only one.

这假设调用InvoicePdf.new(invoice)生成一个文件并返回表示该文件的IO对象。我还注意到你的InvoicePdf类初始化程序需要两个参数,但是你只传递一个。

Update: Also note that Action Mailer will take the file name and work out the mime type, set the Content-Type, Content-Disposition, Content-Transfer-Encoding and base64 encode the contents of the attachment all for you so setting it manually isn't necessary unless you want to override the defaults.

更新:另请注意,Action Mailer将获取文件名并计算mime类型,设置Content-Type,Content-Disposition,Content-Transfer-Encoding和base64编码附件的所有内容,以便手动设置isn除非你想覆盖默认值,否则是必要的。

Based on your pdf generation method, this will probably be better:

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

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

#2


0  

Isnt this just

不仅如此

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

since 3.0?

从3.0开始?

#1


14  

Take a look at the Action Mailer Guide. You need to call the attachments method for you to add an attachment.

看一下Action Mailer Guide。您需要调用附件方法来添加附件。

Try this:

尝试这个:

attachments['attachment_filename'] = InvoicePdf.new(invoice)

This is assuming that calling InvoicePdf.new(invoice) generates a file and returns an IO object representing that file. I also noticed that your InvoicePdf class initializer expects two parameters but you are passing only one.

这假设调用InvoicePdf.new(invoice)生成一个文件并返回表示该文件的IO对象。我还注意到你的InvoicePdf类初始化程序需要两个参数,但是你只传递一个。

Update: Also note that Action Mailer will take the file name and work out the mime type, set the Content-Type, Content-Disposition, Content-Transfer-Encoding and base64 encode the contents of the attachment all for you so setting it manually isn't necessary unless you want to override the defaults.

更新:另请注意,Action Mailer将获取文件名并计算mime类型,设置Content-Type,Content-Disposition,Content-Transfer-Encoding和base64编码附件的所有内容,以便手动设置isn除非你想覆盖默认值,否则是必要的。

Based on your pdf generation method, this will probably be better:

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

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

#2


0  

Isnt this just

不仅如此

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

since 3.0?

从3.0开始?