# Send rendered page as email

**URL:** <https://www.ruby-forum.com/t/send-rendered-page-as-email/127456>\
**Category:** Rails\
**Created:** [January 29, 2008, 12:59pm UTC](https://www.ruby-forum.com/t/send-rendered-page-as-email/127456 "2008-01-29T12:59:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pie-r-squared](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c77e96/32.png) [@pie-r-squared](https://www.ruby-forum.com/u/pie-r-squared)\
**Post date:** [January 29, 2008, 12:59pm UTC](https://www.ruby-forum.com/t/send-rendered-page-as-email/127456/1 "2008-01-29T12:59:35Z")

</div>

I feel like this is actually easy, but I haven’t been able to attack it  
from the right angle. A client wants “send to a friend” functionality  
that  
will allow a user to enter an email address to send the page to, and the  
app will send that rendered page in an email. The big issue I’m seeing  
is  
making sure all the relative URLs (assets and links) get rendered as  
absolute URLs.

Unsurprisingly, the pages should be rendered as if there were no session  
and they are only GETs. I could use an external tool like wget or  
something  
to hit the server and save a “portable” version, but that seems fragile.

Has anyone done this? Can anyone point me to a plugin or something?

Thanks,  
–Greg

---

<div class="post-metadata">

**Author:** ![pie-r-squared](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c77e96/32.png) [@pie-r-squared](https://www.ruby-forum.com/u/pie-r-squared)\
**Post date:** [January 29, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/send-rendered-page-as-email/127456/2 "2008-01-29T15:43:36Z")

</div>

On Jan 29, 2008, at 5:58 AM, Gregory S. wrote:

> absolute URLs.
> 
> Unsurprisingly, the pages should be rendered as if there were no  
> session  
> and they are only GETs. I could use an external tool like wget or  
> something  
> to hit the server and save a “portable” version, but that seems  
> fragile.
> 
> Has anyone done this? Can anyone point me to a plugin or something?

I did something similar once. I have a page that displays either an  
invoice or a receipt which has an option to email it to the  
recipient. I wanted the email to look just like the rendered page,  
so I used render\_to\_string to generate the output and then sent that  
in an html email. My code looks like this

@invoice = Invoice.find(blah blah blah)  
output = render\_to\_string :template =\> ‘invoice/display’

begin  
email = UserMailer.create\_email\_invoice(@invoice, output, :invoice)  
email.set\_content\_type(‘text/html’)  
UserMailer.deliver(email)  
flash[:notice] = “Invoice #{@invoice.invoice\_number} has been  
emailed to #{@invoice.customer.name} (#{@invoice.customer.email}).”  
rescue Exception =\> e  
flash[:color] = ‘red’  
flash[:notice] = e.message  
end

the “output” variable is passed to the UserMailer and get used as the  
body of the message. It’s the same output that would get sent to the  
browser, but with one caveat. In the browser, you have the benefit  
of external CSS and Javascript files. In the HTML email, you have to  
include all of that (as far as I know anyway…maybe there is a way  
to do it).

Hope that helps a least a little.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![pie-r-squared](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c77e96/32.png) [@pie-r-squared](https://www.ruby-forum.com/u/pie-r-squared)\
**Post date:** [January 29, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/send-rendered-page-as-email/127456/3 "2008-01-29T16:36:42Z")

</div>

On Tue, Jan 29, 2008 at 08:42:09AM -0600, Phillip K. wrote:

> > or something to hit the server and save a “portable” version, but that  
> > @invoice = Invoice.find(blah blah blah)  
> > flash[:notice] = e.message  
> > end
> 
> the “output” variable is passed to the UserMailer and get used as the  
> body of the message. It’s the same output that would get sent to the  
> browser, but with one caveat. In the browser, you have the benefit  
> of external CSS and Javascript files. In the HTML email, you have to  
> include all of that (as far as I know anyway…maybe there is a way  
> to do it).
> 
> Hope that helps a least a little.

Well, render\_to\_string is the obvious first step, but the problem is  
those  
assets (and links). I don’t think I mind the email loading the assets  
from  
the server (most email programs will require the user to accept loading  
images/assets from an external site, but that’s fine), but I need a way  
for  
those URLs to be fully-qualified. The HTML BASE tag might do it, but I’m  
not sure. Time to test some email programs (including, ugh, Outlook).

> Peace,  
> Phillip  
> –Greg

---

<div class="post-metadata">

**Author:** ![pie-r-squared](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c77e96/32.png) [@pie-r-squared](https://www.ruby-forum.com/u/pie-r-squared)\
**Post date:** [January 29, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/send-rendered-page-as-email/127456/4 "2008-01-29T17:16:55Z")

</div>

On Tue, Jan 29, 2008 at 10:07:09AM -0600, Phillip K. wrote:

> > > Phillip  
> > > –Greg
> 
> Ah, I see your dilemma. One possibility, though I think it would be  
> a last resort, would be to write a helper to create an absolute URL  
> for all of the resource related helpers (link\_to, image\_tag, etc).  
> I’d like to think there is a simpler solution, though.

I guess another possibility is using Hpricot to run through and make all  
the URLs absolute. Not marvelously efficient, but I don’t expect this to  
be  
a high-volume site. I just have to hit every element and make every src  
and  
href attribute absolute.

> Peace,  
> Phillip  
> –Greg

---

<div class="post-metadata">

**Author:** ![pie-r-squared](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c77e96/32.png) [@pie-r-squared](https://www.ruby-forum.com/u/pie-r-squared)\
**Post date:** [January 29, 2008, 5:07pm UTC](https://www.ruby-forum.com/t/send-rendered-page-as-email/127456/5 "2008-01-29T17:07:29Z")

</div>

On Jan 29, 2008, at 9:35 AM, Gregory S. wrote:

> but I’m  
> not sure. Time to test some email programs (including, ugh, Outlook).
> 
> > Peace,  
> > Phillip  
> > –Greg

Ah, I see your dilemma. One possibility, though I think it would be  
a last resort, would be to write a helper to create an absolute URL  
for all of the resource related helpers (link\_to, image\_tag, etc).  
I’d like to think there is a simpler solution, though.

Peace,  
Phillip
