电子邮件中的大虾pdf附件 [英] Prawn pdf attachments in the email
问题描述
在我的 Rails 应用程序中,我尝试将发票附加到电子邮件中:
In my Rails application, I'm trying to attach the invoice to the email:
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 文档:
The InvoicePdf is a Prawn PDF document:
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.
我使用的 Rails 版本是 3.0.x
The Rails version I'm using is 3.0.x
推荐答案
查看 行动邮寄指南.您需要调用attachments 方法来添加附件.
Take a look at the Action Mailer Guide. You need to call the attachments method for you to add an attachment.
试试这个:
attachments['attachment_filename'] = InvoicePdf.new(invoice)
这是假设调用 InvoicePdf.new(invoice) 生成一个文件并返回代表该文件的 IO 对象.我还注意到您的 InvoicePdf 类初始值设定项需要两个参数,但您只传递了一个.
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.
更新:另请注意,Action Mailer 将获取文件名并计算出 mime 类型,设置 Content-Type、Content-Disposition、Content-Transfer-Encoding 和 base64 编码附件的所有内容,因此手动设置不是除非您想覆盖默认值,否则是必需的.
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.
根据您的 pdf 生成方法,这可能会更好:
Based on your pdf generation method, this will probably be better:
invoice = InvoicePdf.new(invoice)
attachments["invoice.pdf"] = { :mime_type => 'application/pdf', :content => invoice.render }
mail(:to => @user.email, :subject => "Your Invoice")
这篇关于电子邮件中的大虾pdf附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!