Prawn pdf attachments in the email

前端 未结 2 1024
眼角桃花
眼角桃花 2021-02-03 10:27

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

def invoice(invoice)
  attachment :content_disposition => \"attachment\",
                    


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 11:10

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

    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.

    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.

    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")
    

提交回复
热议问题