[LRUG] email attachments (generated from a template) in rails

Ed Davey ed at veryreal.co.uk
Tue Feb 26 10:00:23 PST 2008

I have two related questions about attaching a file in rails: I want  
to generate a delivery note in html from an rhtml template and then  
attach it to a plain text email.

It basically works:

     @subject      = "Paperfly order # #{order.id}"
     @recipients   = 'test at paperfly.co.uk'
     @from         = 'sales at paperfly.co.uk'
     @sent_on      = Time.now
     @body["order"] = order

     part :content_type => "text/plain",
          :body => render_message("packing_note", :order => order)

     rhtml_file = "<h1>Packing Note for order number #{order.id}</h1>  
etc etc"

     attachment :content_type  =>  "text/html",
                :body => rhtml_file,
                :filename => "delivery_note_for_order_#{order.id}.html"

1) where do I put my rhmtl template other than inline?
  If I try:

rhtml_file = IO.read(RAILS_ROOT + "/app/views/order_mailer/ 
packing_note_attachment.rhtml") then all my string interpolation gets  
messed up:

  "<h1>Packing Note for order number <%= \#{order.id} %></h1> etc  

2) how do I test emails with attachments?
When I do something like:

assert_match(/9103/, response.body)

The test reports that there's nothing there even though the match  
*is* there in the log to rail_mail and in reality if I send a actual  
test message through. (I understand that there was (is?) a bug in 1- 
ish Rails which is why I'm using "part" explicity as per the note in  
AWDWR. I'm using Rails 1.2.3 here.)

What is the syntax to check for the existence of an attachment with a  
certain name, for example?

Thanks for any pointers.


More information about the Chat mailing list