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

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.html
      format.json
      format.pdf do
        pdf = BefriendingReferralForm.new(@current_referrer)
        send_data pdf.render, filename: 'referrer.pdf',
                              type: 'application/pdf',
                              disposition: :inline
      end
    end

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)
    coach.assign_attributes(coach_params)

    if coach.save
        thinking adding code here?
      redirect_to referrers_root_path
    else
      render :new
    end
  end

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'] = BefriendingReferralForm.new(@current_referrer).render
    mail(:to => recipient, :subject => "New account information")
  end
end

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: http://guides.rubyonrails.org/action_mailer_basics.html


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.