Attach a PDF File to an Email

Hello ~

I have an email that I need to attach a PDF to. The email is being
sent, and a file is attached but it is not the original file. I am
following the example:

attachment :content_type => “image/jpeg”, :body =>
File.read(“an-image.jpg”)

changing it to:

attachment :content_type => “application/pdf”, :body =>
File.read(“pdf/my.pdf”)

There is also this example:

attachment “application/pdf” do |a|
a.body = get_pdf()
end

I tried this method passing the file object back from the get_pdf(),
but that also is not working. Is there something I am missing? Is
there more documentation on attachments for action mailer? So far my
only reference has been the wiki and
http://api.rubyonrails.com/classes/ActionMailer/Base.html

Thx,


Ben R.
303-947-0446
http://www.benr75.com

I finally got the PDF attachments working by changing:

attachment :content_type => “application/pdf”, :body =>
File.read(“pdf/my.pdf”)

to

attachment :content_type => “application/pdf”, :body =>
File.read(“#{RAILS_ROOT}/public/pdf/my.pdf”), :filename => “my.pdf”

Now, this method still DOES NOT work for some reason on my Windows
development box. The attachment comes through on the email, but is
corrupted. I was getting no where so I put it on my production box
(Gentoo Linux) and it worked right away. Go figure.

~ Ben

On 5/5/06, Ben R. [email protected] wrote:

attachment :content_type => “application/pdf”, :body => File.read(“pdf/my.pdf”)
only reference has been the wiki and
Peak Obsession

Thx,


Ben R.
303-947-0446
http://www.benr75.com


Ben R.
303-947-0446
http://www.benr75.com

<…>

Now, this method still DOES NOT work for some reason on my Windows
development box. The attachment comes through on the email, but is
corrupted. I was getting no where so I put it on my production box
(Gentoo Linux) and it worked right away. Go figure.
<…>

Just guessing - has it anything to do with “binmode”?

Regards,
Rimantas

http://rimantas.com/

Community and/or Ben,

Did you ever get this working on your Windows box? I just posted a
question last night with the same issue. I, too, have a windows box and
am having the same corrupt file problem.

Please advise…

Michael

Ben R. [email protected] wrote:
I finally got the PDF attachments working by changing:

attachment :content_type => “application/pdf”, :body =>
File.read(“pdf/my.pdf”)

to

attachment :content_type => “application/pdf”, :body =>
File.read(“#{RAILS_ROOT}/public/pdf/my.pdf”), :filename => “my.pdf”

Now, this method still DOES NOT work for some reason on my Windows
development box. The attachment comes through on the email, but is
corrupted. I was getting no where so I put it on my production box
(Gentoo Linux) and it worked right away. Go figure.

~ Ben

On 5/5/06, Ben R. wrote:

attachment :content_type => “application/pdf”, :body => File.read(“pdf/my.pdf”)
only reference has been the wiki and
Peak Obsession

Thx,


Ben R.
303-947-0446
http://www.benr75.com


Ben R.
303-947-0446
http://www.benr75.com

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 and/or Ben,

Did you ever get this working on your Windows box? I just posted a
question last night with the same issue. I, too, have a windows box and
am having the same corrupt file problem.

Please advise…

Michael

Ben R. [email protected] wrote:
I finally got the PDF attachments working by changing:

attachment :content_type => “application/pdf”, :body =>
File.read(“pdf/my.pdf”)

to

attachment :content_type => “application/pdf”, :body =>
File.read(“#{RAILS_ROOT}/public/pdf/my.pdf”), :filename => “my.pdf”

Now, this method still DOES NOT work for some reason on my Windows
development box. The attachment comes through on the email, but is
corrupted. I was getting no where so I put it on my production box
(Gentoo Linux) and it worked right away. Go figure.

~ Ben

On 5/5/06, Ben R. wrote:

attachment :content_type => “application/pdf”, :body => File.read(“pdf/my.pdf”)
only reference has been the wiki and
Peak Obsession

Thx,


Ben R.
303-947-0446
http://www.benr75.com


Ben R.
303-947-0446
http://www.benr75.com


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail
Beta._______________________________________________
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

In case someone else comes across this issue (corrupt email
attachments).

It is a bin mode property problem as Rimantas suggests. By default
File.read on linux will open in Binary mode in Windows it will open in
Text mode.

To fix just change file.read to the following:

attachment “application/pdf” do |a|
a.filename = File.basename(file_path)
File.open(file_path, ‘rb’) do |file|
a.body = file.read
end
end

The ‘rb’ option opens the file in binary mode - see here for other
options:

http://ruby-doc.org/core/classes/IO.html

Cheers
Luke