Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I send Prawn pdf via email

Neil Patel asked in Rails

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
Reply

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.

Reply

ok cool thanks Chris

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

Reply

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 80,973+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.