views:

81

answers:

2

My application creates a .pdf file when it is rendered by passing it to the URL (for example, domain.com/letter/2.pdf)

It doesn't get saved anywhere.

How can I make that actual pdf an attachment in an outbound email.

Here is my mailer:

  def campaign_email(contact,email)
    subject    email.subject
    recipients contact.email
    from       'Me <[email protected]>'
    sent_on    Date.today

    body       :email => email
  end
+2  A: 

Here's an example for rails 2

class ApplicationMailer < ActionMailer::Base
# attachments
def signup_notification(recipient, letter)
  recipients      recipient.email_address_with_name
  subject         "New account information"
  from            "[email protected]"

  attachment :content_type => "image/jpeg",
    :body => File.read("an-image.jpg")

  attachment "application/pdf" do |a|
    a.body = letter
  end
end
end

in your view or wherever your calling your method:

ApplicationMailer.deliver_signup_notification(letter)
jtmkrueger
what do you mean by generate my pdf there....would it be the path?
Angela
that, or the method your using to create your pdf
jtmkrueger
This is the path in the create method: contact_letter_path(@contact_letter, :format => 'pdf') is that what I put there?
Angela
contact_letter_path puts out what you want, so: letter = contact_letter_path(@contact_letter, :format => 'pdf')
jtmkrueger
then pass letter to ActionMailer: I made edits to my response
jtmkrueger
you mean, letter as an object?
Angela
A: 

one quick an easy solution would be fetch the url content using net/http and open-uri, to get the attachment

attachments['free_book.pdf'] = open("http://#{request.host}/letter/#{id}.pdf")

eg:

def campaign_email(contact,email)
    subject    email.subject
    recipients contact.email
    attachments['free_book.pdf'] = open("http://#{request.host}/letter/#{id}.pdf")
    from       'Me <[email protected]>'
    sent_on    Date.today

    body       :email => email
end

or, call the PDF generation inside your mailer controller action

vrsmn