Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > ActionMailer PDF attachment and Windows

Reply
Thread Tools

ActionMailer PDF attachment and Windows

 
 
dkmd_nielsen
Guest
Posts: n/a
 
      12-14-2009
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
(<filename>)" to do the attachment. The attached file was always
corrupt. I attempted to simply copy the file using File.read
(<filename>), 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,fatt ach)
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
 
Reply With Quote
 
 
 
 
Gregory Brown
Guest
Posts: n/a
 
      12-15-2009
On Mon, Dec 14, 2009 at 1:56 PM, dkmd_nielsen <(E-Mail Removed)> 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. =A0All the examples
> on the web I could find instructed me to use ":body =3D> File.read
> (<filename>)" to do the attachment. =A0The attached file was always
> corrupt. =A0I attempted to simply copy the file using File.read
> (<filename>), and the copy too yielded a truncated file.
>
> What just now worked for me was a different body syntax. =A0What works
> is the following: =A0":body =3D> File.new(fattach,'rb').read()". =A0I 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ActionMailer,Templates (erb) and aweson_mail without Rails Pablo Q. Ruby 1 08-22-2008 05:43 PM
PDF::Writer, create pdf and insert in other pdf file. Ricardo Pog Ruby 1 03-26-2008 08:24 PM
ActionMailer Inline Image Attachment Issue beanbrown@gmail.com Ruby 0 02-10-2006 07:37 AM
ActionMailer Kevin Brown Ruby 2 11-20-2005 08:33 AM
ActionMailer NoMethodError 'deliver' enspired Ruby 3 07-28-2005 03:18 PM



Advertisments