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
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:> 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
>
--
Ben Reubenstein
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 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:> 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
>
--
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060715/6f94ba80/attachment.html
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:> 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
>
--
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
---------------------------------
Do you Yahoo!?
Next-gen email? Have it all with the all-new Yahoo! Mail Beta.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060715/91d227eb/attachment.html
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---