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

I am trying to figure out how I would create a CGI to mail me everytime my page is visited. I assume it would be like a counter page except I dont want a count. I want an email on every page hit. Please advise how I can do this? I understand the mail part but how would I get it to email everytime a page was visited?
use strict; my page_visited = 'http://companypage/index.html'; If(page_visited) { use Mail::Sendmail; my %mail = ( To => 'jones@company.com', From => 'smith@company.com', Subject => "Page visited", Message => "Page was visited", ); $mail{smtp} = '111.22.333.444'; sendmail(%mail) || die "\nProblem! $Mail::Sendmail::error\n"; }

Replies are listed 'Best First'.
Re: Email on every page hit
by bear0053 (Hermit) on Dec 05, 2003 at 19:37 UTC
    you can embed a cgi script call in every page by doing something like this:
    <img src="http://www.site.com/cgi-bin/email_page_hit.cgi?page=NAMEOFPA +GE.htm">
    now everytime your page is loaded your script will be called and an email will be sent to you notifying you.

    Keep in mind that your script must return an image...what you can do is return a blank 1x1 pixel image so that nothing is displayed where it is called. have email_page_hit.cgi return an image like this:
    print "Expires: Fri, 30 Oct 1998 14:19:41 GMT\n"; print "Content-type:image/png\n\n"; open IMAGE, "PATH_TO_FILE/invisible.png" or die "ERROR $!"; binmode IMAGE; binmode STDOUT; my $buff; while(read IMAGE, $buff, 1024) { print $buff; } close IMAGE;
    this code snippet will have your cgi print out an image in place of the html <img> tag

      Well, that will work fine until you get someone who chooses not to display graphics -- either because they have that setting in Opera or Mozilla, or because they're using Lynx, or because they're using a browser that 'speaks' the pages to them (because they're blind).

      --t. alex
      Life is short: get busy!
Re: Email on every page hit
by talexb (Chancellor) on Dec 05, 2003 at 19:34 UTC

    If you want the E-Mail to go out to you every time the page is loaded, you'd have to do some fiddling in the Apache server configuration, or use a CGI to display the page, then send you E-Mail, all in one shot.

    Alternatively, you could always parse the log file every few hours and blast off a summary E-Mail if you could get away without doing a 'real time' solution.

    --t. alex
    Life is short: get busy!
Re: Email on every page hit (OT)
by bart (Canon) on Dec 05, 2003 at 22:55 UTC
    I want an email on every page hit.
    Boy you really don't expect your page to be popular, do you?

    If I were you, I'd prepare myself for a popularity burst, collect info per page hit, and send the mail only as a summary, once an hour or so, and only if there are any hits. You could slow down the cycle if you want fewer mails and don't care so much about the delay.

    The basic idea is that you log every hit in a dedicated log file, and set up a cron job to check the log, and mail you the summary.

    Update: Oh you're on Windows, so you'll have to figure out a Windows alternative for cron.

Re: Email on every page hit
by mpeppler (Vicar) on Dec 05, 2003 at 19:38 UTC
    I'm not sure that this is really a good idea. If you really want to do this then how you'd do this depends on your setup.

    If you don't have any control over the actual web server, then I would a an embedded 1 bit image tag in the static page, and have that img src be a link to your CGI script. The CGI script will the output the actual image (1x1 transparent gif, for example), and uses the referer to know what page it was called from and then sends the email.

    Michael

      thanks, I forgot to say this is on an Windows 2000 server with IIS.
Re: Email on every page hit
by tcf22 (Priest) on Dec 05, 2003 at 19:40 UTC
    If your page is HTML, which is what my page_visited = 'http://companypage/index.html'; leads me to believe.

    You could do something like:
    #! /usr/bin/perl -T use strict; use CGI; my $cgi = new CGI; my $page = $cgi->param('page'); my %mail = ( To => 'jones@company.com', From => 'smith@company.com', Subject => "$page visited", Message => "$page was visited", smtp => '111.222.333.444' ); &sendmail(\%mail); ##************************************************ ##Do checking on the parameter for security reasons ##************************************************ print $cgi->header(); open(FILE, "/path/to/html/$page.htm"); print while(<FILE); close(FILE);
    This is a simple example, I would look into using HTML::Template instead.

    - Tom

Re: Email on every page hit
by CountZero (Bishop) on Dec 06, 2003 at 07:42 UTC
    On a more philosofical level, I wonder what you are trying to accomplish?

    All you get is an email for each page hit

    You will find a lot more info in the log-file which is kept by the server.

    This looks a lot like stupid homework with hardly any relevance to real-world situations. (O, how I hated to do such stupid homework tasks.)

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Email on every page hit
by holo (Monk) on Dec 05, 2003 at 19:30 UTC

    That depends on what you are using to generate the page. ASP, Embperl, CGI (pure perl), or what ? You just need to create a routine with the above code wrapped in and call it from the page you want to trigger this email.

    You might also want to add a handler in Apache (You're using Apache aren't you ?) but that's OT isn't it ;) ?

      It will probably be an HTML page. Please advise further if possible. Thanks.

        for plain html files, you just need to install a PerlFixupHandler for the files you need to trigger.

        <Files ~ "\.html$"> PerlFixupHandler MyPackage::MailMe; </Files>

        Create a package somewhere in @INC that says.

        package MyPackage::MailMe; use Apache::RequestIO; use Apache::RequestRec; use Apache::Const -compile => 'OK'; use Mail::Sendmail; sub handler { my $r = shift; sendMail($r->uri); Apache::OK; } sub sendMail { my $page_visited = shift; my %mail = ( To => 'jones@company.com', From => 'smith@company.com', Subject => "Page visited", Message => "$page_visited was visited", ); $mail{smtp} = '111.22.333.444'; sendmail(%mail) || die "\nProblem! $Mail::Sendmail::error\n"; }

        This is untested and incomplete. Read the modperl docs to find out more.

Re: Email on every page hit
by TomDLux (Vicar) on Dec 06, 2003 at 16:12 UTC

    According to [http://www.carleton.ca/~dmcfet/html/ssi.html#email|this documentation' from Carlton University, Server Side Includes have a tag to generate email. Since I haven't seen it mentioned elsewhere, this may be one of the ones only provided by Webstar web servers.

    But if it's provided on your system, you don't have to do a thing, just provide the tag and leave all the work to the server.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Email on every page hit
by Anonymous Monk on Dec 06, 2003 at 09:15 UTC
    If you really are desparate for some email, just post your addy anywhere on USENET. No coding required.