Community, I''m struggling with an issue and I''m sure it is trivial but I cannot find the solution. I''m trying to attach a PDF file to an email. For simplicity, I have explicitely named a given file but when I get the email, the attachment is only a partial of the document which causes it to be corrupt. Sample below: @recipients = ''test@testrecipient.com'' @from = "Test <test@mydomain.com>" @subject = "My Subject" fileName = "#{RAILS_ROOT}/documents/test.pdf" attachment :content_type => "application/pdf", :content_disposition => "attachment", :body => File.read(fileName) I''m using the testMail = MyMailer.create_test_mail followed by MyMailer.deliver(testMail) process and the email is being sent, along with an attachment but the attachment is only about half of the actual original file size. The beginning part of the file appears to be correct - but for some reason, the entire file isn''t being sent. Any ideas? Also, how do I name the attachment? It is coming across as Part 1.1. Your help is greatly appreciated! I have followed various examples found on the web and ROR site but apparently am missing something basic! Thanks, Michael --------------------------------- See the all-new, redesigned Yahoo.com. Check it out. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060715/378e1105/attachment.html
Michael
2006-Jul-15 19:31 UTC
[Rails] ActionMailer and PDF Attachment and corrupt file? - WORKAROUND
FYI, I was able to solve my problem by changing strategy a little. Instead of saving the rendered PDF to a file and then using that file in the attachment I render it in memory and pass that to ActionMailer. This leads me to believe the problem is with File.read and based on earlier posts, probably limited to Windows machines. Sample: From the controller: # p.render is the pdfwriter method to take my generated PDF and render it in memory my_email = MyMailer.create_test_email(@order, p.render) MyMailer.deliver(my_email) From MyMailer: def test_email(order, pdfFile) @recipients = order.email_address @from = "UserName <testsender@mydomain.com>" @subject = "Test Email Attachment" attachment :content_disposition => "attachment", :body => pdfFile, :content_type => "application/pdf", :filename => ''whatever_name_you_want.pdf'' end Hope this helps someone avoid the same problem that I had with the incomplete files being read in with File.read. Regards, Michael Michael <codeslush@yahoo.com> wrote: Community, I''m struggling with an issue and I''m sure it is trivial but I cannot find the solution. I''m trying to attach a PDF file to an email. For simplicity, I have explicitely named a given file but when I get the email, the attachment is only a partial of the document which causes it to be corrupt. Sample below: @recipients = ''test@testrecipient.com'' @from = "Test <test@mydomain.com>" @subject = "My Subject" fileName = "#{RAILS_ROOT}/documents/test.pdf" attachment :content_type => "application/pdf", :content_disposition => "attachment", :body => File.read(fileName) I''m using the testMail = MyMailer.create_test_mail followed by MyMailer.deliver(testMail) process and the email is being sent, along with an attachment but the attachment is only about half of the actual original file size. The beginning part of the file appears to be correct - but for some reason, the entire file isn''t being sent. Any ideas? Also, how do I name the attachment? It is coming across as Part 1.1. Your help is greatly appreciated! I have followed various examples found on the web and ROR site but apparently am missing something basic! Thanks, Michael --------------------------------- See the all-new, redesigned Yahoo.com. Check it out. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails --------------------------------- Yahoo! Music Unlimited - Access over 1 million songs.Try it free. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060715/adbc4ff4/attachment-0001.html