ActionMailer and PDF Attachment and corrupt file?

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 = ‘[email protected]
@from = “Test [email protected]
@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

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 [email protected]
@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 [email protected] 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 = ‘[email protected]
@from = “Test [email protected]
@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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs