Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

CSS for printing and tell-a-friend scripts

by mrtrinity3 (Initiate)
on Dec 07, 2004 at 14:46 UTC ( [id://412913]=perlquestion: print w/replies, xml ) Need Help??

mrtrinity3 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm a newbie, so I apologize if I'm taking up anybody's time with these questions, but I haven't found a good solution to them yet, and I would greatly appreciate any helpful advice. First, I'm trying to add a tell-a-friend script to my website that allows the user to type in any e-mail address they choose and send the url of a selected page, along with a message that they type, to that person. I have a lot of pages, so it would help if I could have a script so that I can just put a link onto all of my pages, and that link takes the browsers to a universal tell-a-friend page, and the url of the page which they clicked the link is inputed. ( I hope that makes sense) Second, I would like to know the easiest way to make printer-friendly pages, without having to make duplicates of all of the pages on my website. Thank you for your time, and your help...

2004-12-08 Edited by Arunbear: Changed title from 'newbie: Just two questions', as per Monastery guidelines

  • Comment on CSS for printing and tell-a-friend scripts

Replies are listed 'Best First'.
Re: CSS for printing and tell-a-friend scripts
by gaal (Parson) on Dec 07, 2004 at 15:08 UTC
    Tell-a-friend can be dangerous: if a spammer finds your form, they can use *your* computer resources to send out lots of spam -- which will be traced back to you. Only allow authenticated users to use this feature, and make some provision to limit the amount of messages each user is allowed to send. You have been warned!

    As for how to send the actual message, there are plenty of mail modules on CPAN. I think MIME::Lite can do what you want with a minimum of fuss.

    Adding a link to your existing pages depends on how your site is designed. If you're using any kind of templating system, it's easy -- add it once to the appropriate template. If you aren't -- then you should :-) but you might, in a pinch, do a quick search and replace for <BODY> (or some other location that exists in your pages), and put <BODY><A HREF="your page">tell a friend</a>. There are perl one-liners that can do that for you.

    Printer-friendly pages? The answer again depends on how your site is designed, but if the data is coming from a database and then rendered by a template, just make a "printer" version of the template that doesn't include the printer-unfriendly elements :)

    Update: eieio's suggestion re: CSS is a very good one, too.

Re: CSS for printing and tell-a-friend scripts
by eieio (Pilgrim) on Dec 07, 2004 at 15:03 UTC
    To address the second question, use a print-specific Cascading Style Sheets to make print-friendly pages. The media type specifies which style sheet should be used. For example:
    <link rel="stylesheet" media="screen" href="/screen.css" type="text +/css" /> <link rel="stylesheet" media="print" href="/print.css" type="text/c +ss" />
    This allows you to have a page that is rendered based on the media type specified by the browser.
Re: CSS for printing and tell-a-friend scripts
by gellyfish (Monsignor) on Dec 07, 2004 at 15:17 UTC
    As gaal pointed out allowing the input of arbitrary text into an e-mail to be sent to an arbitrary address will result in your program being exploited to send spam. You don't want this, your ISP doesn't want it and we are not going to encourage it.

    If you are willing to forego the 'message they type' bit then you might as well use something like the NMS TFMail which is designed to allow you to do this in a safe way.

    /J\

Re: CSS for printing and tell-a-friend scripts
by amrangaye (Friar) on Dec 07, 2004 at 15:11 UTC
    Hi
    Here are some answers to your questions:

    1. For the tell-a-friend script you might use a combination of CGI (to display the page and get the friedn's email address from the user), and one of the Mail modules from CPAN to send the email. As always, do research on these, write some code, and post any further question you have. People here will gladly answer them.
    2. For printer-friendly pages, one way is to use CSS (Cascading Style Sheet). IMSMR, you can put something like:
      media:print{ color: black; }
      in the CSS section of your page. Replace color:black with whatever you want your printed pages to look like. Most modern browsers recognize this and will use it when printing.

    Cheers!
Re: CSS for printing and tell-a-friend scripts
by blue_cowdawg (Monsignor) on Dec 07, 2004 at 16:09 UTC

        Hi, I'm a newbie, so I apologize if I'm taking up anybody's time with these questions,

    My first bit of advice so you kind sir or madam is to not lead off a question like that. It tends to grate on folks and may result in questions going unanswered. IOW: don't grovel! :-)

    First I'd recommend you go on over to the Tutorials page and learn how to research for answers on your own.

    The first question. BIG HINT: what you are looking for is how to read the CGI headers and find what page has referred the browser to your script. IIRC the variable is HTTP_REFERRER (fat fingers!) HTTP_REFERER and you should be able to get it off the %ENV hash.

    Here is an html you can use to test what I am talking about:

    <!-- HTML to reference our script --> <a href="/cgi-bin/test.pl">Click Me</a>
    and a Perl/CGI script with very minimal functionality:
    #!/usr/bin/perl printf "Content-type: text/html\n\n\n"; printf "%s -- %s<br>\n",$_,$ENV{$_} foreach sort keys %ENV;

    CAVEAT: Don't leave that code on your production environment as there is some serious information leaking going on there. It is there for demonstration purposes only.

    Once you figure out where the user is coming from page wize the rest is using Mail::Send or MIME::Lite or other CPAN module to to send your mail after you have processed it with CGI and some glue code.

    The second question. Other monks here have done a bang up job answering that one. The answer lies in the use of CSS and the media attribute (directive?).

    With that information in your hands go forth, code and conquor!

      The first question. BIG HINT: what you are looking for is how to read the CGI headers and find what page has referred the browser to your script. IIRC the variable is HTTP_REFERRER and you should be able to get it off the %ENV hash.
      I believe if you'll look at the spec, you'll find that it's actually HTTP_REFERER.
Re: CSS for printing and tell-a-friend scripts
by csuhockey3 (Curate) on Dec 07, 2004 at 15:52 UTC
    This thread about form to email will be of some help to you. Also have a look at the tutorials section here on PM, great stuff there for you as well.
Re: CSS for printing and tell-a-friend scripts
by mpeters (Chaplain) on Dec 07, 2004 at 16:30 UTC
    In addition to all the good advice you have recieved so far, I would check out this module: CGI::Application::MailPage It uses CGI::Application so you can subclass it to override some portions, but your instance script should be very small.
Re: CSS for printing and tell-a-friend scripts
by sgifford (Prior) on Dec 07, 2004 at 17:10 UTC
    To find out where the user came from, you can look at the Referer header, although you can't rely on this always being set. For a more reliable method, embed the current URL into the request, via a CGI parameter:
    <!-- On page /example/page1.html --> <a href="/cgi-bin/tellafriend?url=/example/page1.html"> Tell a Friend! </a>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://412913]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-16 20:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found