Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

How do I send Prawn pdf via email

Rails • Asked by Neil Patel

Hello Everyone

how do i attaced a prawn pdf to email . I have implemented the prawn gem
so when you generate a new form and add the .pdf ot gives me the PDF , which works. but now when the form is created i want it to send an email with the PDF attached?

def new
    coach.build_referral(referrer: @current_referrer)

    respond_to do |format|
      format.pdf do
        pdf =
        send_data pdf.render, filename: 'referrer.pdf',
                              type: 'application/pdf',
                              disposition: :inline

but i would like to send an email when the form is submitted , i was thinking of doing this in the create method, This my current create method.

  def create
    coach.build_referral(referrer: @current_referrer)

        thinking adding code here?
      redirect_to referrers_root_path
      render :new

Hey Neil!

Attachments are pretty easy. You use the attachments method on the email and give it a file object.

class ReferralMailer < ActionMailer::Base
  def referral(recipient)
    attachments['referral_form.pdf'] =
    mail(:to => recipient, :subject => "New account information")

Since you're rendering your own file, you just pass that in and Rails should take care of the rest.

ok cool thanks Chris

how would I call the ReferralMailer method in the my controller?
with the new and create action?

Just like you would do with sending any other email. You can read more on the guides page:

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 29,763+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.