# ActionMailer and PDF Attachment and corrupt file?

**URL:** <https://www.ruby-forum.com/t/actionmailer-and-pdf-attachment-and-corrupt-file/98514>\
**Category:** Rails\
**Created:** [July 15, 2006, 9:38am UTC](https://www.ruby-forum.com/t/actionmailer-and-pdf-attachment-and-corrupt-file/98514 "2006-07-15T09:38:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [July 15, 2006, 9:38am UTC](https://www.ruby-forum.com/t/actionmailer-and-pdf-attachment-and-corrupt-file/98514/1 "2006-07-15T09:38:40Z")

</div>

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 = ‘removed\_email\_address@domain.invalid’  
@from = “Test [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”  
@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

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [July 15, 2006, 9:34pm UTC](https://www.ruby-forum.com/t/actionmailer-and-pdf-attachment-and-corrupt-file/98514/2 "2006-07-15T21:34:33Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”  
@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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 = ‘removed\_email\_address@domain.invalid’  
@from = “Test [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”  
@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](http://Yahoo.com). Check it out.

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
