Thursday 4 June 2009

Send email with PDF attachment in Rails


I am trying to send an email with a PDF attachment via Action Mailer, but somehow when opening email the attachment was corrupted. Been banging my head against the wall for few hours, finally I realised it was because of my Windows development box (I hate you, windows).


So, by default File.read on Linux will open the file in Binary mode in but in Text mode on Windows. To fix this I need to open the file in binary mode before I read it in:

attachment "application/pdf" do |a|
a.filename = File.basename(order_form_content)
File.open(order_form_content, 'rb') do |file|
a.body = file.read
end
end

1 comment: