Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Email a pdf generated with prawn to an email sent by ActionMailer?

I have an ecommerce app. I'm using Prawn to generate pdf invoices of orders. I'm using a standard Prawn setup. In views/admin/orders, I have a file called show.pdf.prawn. When the seller is viewing an order in his admin section, he clicks a link that opens the pdf version of the orders/show view. This all works perfectly.

Now, the tricky part. When an order is completed, I send an email to the seller. What I'd like to do is attach the pdf invoice version of orders/show to that email. Is it possible to do this? The documentation on email attachments is pretty limited and I haven't been able to find resources that go through the workflow that I'm describing.

Any guidance is appreciated.

like image 763
MikeH Avatar asked Jan 19 '26 08:01

MikeH


1 Answers

Sending an attachment with an email is fairly easy with ActionMailer:

class InvoiceMailer < ActionMailer::Base

  def email_with_attachment(pdf_invoice)
    .
    .
    .

    attachment "application/pdf" do |a|
      a.filename = "some_invoice.pdf"
      a.body = pdf_invoice
    end
  end

end

One problem you might have with this is generating the pdf file outside of the prawnto method (when using the prawnto plugin)- If this is is the case I strongly recommend you to use this approach instead.

like image 64
Milan Novota Avatar answered Jan 23 '26 21:01

Milan Novota



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!