Forum: Ruby on Rails Attach a PDF File to an Email

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstein (Guest)
on 2006-05-05 19:29
(Received via mailing list)
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 Reubenstein
303-947-0446
http://www.benr75.com
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstein (Guest)
on 2006-05-08 18:57
(Received via mailing list)
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 Reubenstein <benr@x-cr.com> wrote:
> attachment :content_type => "application/pdf", :body => File.read("pdf/my.pdf")
> only reference has been the wiki and
> http://api.rubyonrails.com/classes/ActionMailer/Base.html
>
> Thx,
>
> --
> Ben Reubenstein
> 303-947-0446
> http://www.benr75.com
>


--
Ben Reubenstein
303-947-0446
http://www.benr75.com
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-05-09 01:39
(Received via mailing list)
<...>
> 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/
5565e460a42fbe5669b458b2753f8d34?d=identicon&s=25 Michael (Guest)
on 2006-07-15 18:51
(Received via mailing list)
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 Reubenstein <benr@x-cr.com> 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 Reubenstein wrote:
> attachment :content_type => "application/pdf", :body => File.read("pdf/my.pdf")
> only reference has been the wiki and
> http://api.rubyonrails.com/classes/ActionMailer/Base.html
>
> Thx,
>
> --
> Ben Reubenstein
> 303-947-0446
> http://www.benr75.com
>


--
Ben Reubenstein
303-947-0446
http://www.benr75.com
5565e460a42fbe5669b458b2753f8d34?d=identicon&s=25 Michael (Guest)
on 2006-07-15 21:31
(Received via mailing list)
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 <testsender@mydomain.com>"
    @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 <codeslush@yahoo.com> 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 Reubenstein <benr@x-cr.com> 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 Reubenstein wrote:
> attachment :content_type => "application/pdf", :body => File.read("pdf/my.pdf")
> only reference has been the wiki and
> http://api.rubyonrails.com/classes/ActionMailer/Base.html
>
> Thx,
>
> --
> Ben Reubenstein
> 303-947-0446
> http://www.benr75.com
>


--
Ben Reubenstein
303-947-0446
http://www.benr75.com
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails


---------------------------------
  Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail
Beta._______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
85f8b7bbe07bff22e0b38cca818cf692?d=identicon&s=25 Luke Pearce (kule)
on 2007-12-02 20:23
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
This topic is locked and can not be replied to.