# ActionMailer PDF attachment and Windows

**URL:** <https://www.ruby-forum.com/t/actionmailer-pdf-attachment-and-windows/180209>\
**Category:** Ruby\
**Created:** [December 14, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/actionmailer-pdf-attachment-and-windows/180209 "2009-12-14T19:58:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [December 14, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/actionmailer-pdf-attachment-and-windows/180209/1 "2009-12-14T19:58:10Z")

</div>

I’ve been struggling for a couple of days down with trying to attach a  
pdf file to an email using ActionMailer on Windows. All the examples  
on the web I could find instructed me to use “:body =\> File.read  
()” to do the attachment. The attached file was always  
corrupt. I attempted to simply copy the file using File.read  
(), and the copy too yielded a truncated file.

What just now worked for me was a different body syntax. What works  
is the following: “:body =\> File.new(fattach,‘rb’).read()”. I don’t  
exactly understand the significance of the of the difference. Nor do  
I understand why the original example above works in Linux but not  
Windows, and why the latter example works in Windows. But it does  
work. I just want to get discussion on the web in case someone else  
encounters the same problem, and this provides an alternative  
solution.

class Emailer \< ActionMailer::Base  
def report\_attachment(to,cc,bcc,from,subject,body,fattach)  
pp to,cc,bcc,from,subject,body,fattach  
recipients to  
cc cc  
bcc bcc  
from from  
subject subject  
body body

```
  attachment :content_type => "application/pdf",
    :content_disposition => "attachment",
    :filename => File.basename(fattach),
    :body => File.new(fattach,'rb').read()

```

end  
end

---

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [December 15, 2009, 3:04am UTC](https://www.ruby-forum.com/t/actionmailer-pdf-attachment-and-windows/180209/2 "2009-12-15T03:04:16Z")

</div>

On Mon, Dec 14, 2009 at 1:56 PM, dkmd\_nielsen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’ve been struggling for a couple of days down with trying to attach a  
> pdf file to an email using ActionMailer on Windows. All the examples  
> on the web I could find instructed me to use “:body =\> File.read  
> ()” to do the attachment. The attached file was always  
> corrupt. I attempted to simply copy the file using File.read  
> (), and the copy too yielded a truncated file.
> 
> What just now worked for me was a different body syntax. What works  
> is the following: “:body =\> File.new(fattach,‘rb’).read()”. I don’t  
> exactly understand the significance of the of the difference.

When using Ruby 1.8 on Windows (and everywhere on Ruby 1.9 – for a  
different reason), if you want to work with binary files, File.read()  
is never safe. You need to tell Ruby you’re working with a binary  
file by using the “rb” access mode. But you might consider doing it  
this way:

File.open(filename, “rb”) { |f| f.read } so that your file handle is  
closed properly. On Ruby 1.9, File.binread(filename) can also be  
used.

-greg
