Emailing web site stats once a month?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • james
    Senior Member
    • Mar 2004
    • 183

    #1

    Emailing web site stats once a month?

    Hi,

    I don't know if this is possible, but I'd like to be able to email to some of my clients a web site hits summary at the end of each month.

    Ideally, if it is possible, to email them a copy of the awstats page for the current month.

    Most of my clients are too inexperienced to log into cpanel and look themselves, so if i was able to email them this then I think they would be thankful for it.

    I would set the script up as a cron job to run once a month, but I'm stuck for ideas for getting the script working.

    Any advice/help would be appreciated.

    Cheers,

    James

    -------------------
    EDIT: I've now developed a script to achieve this:


    The script allows you to input an account's information (username, password, domain name) into a form, and then the awstats statistics are read from the server and emailed to the address specified.

    If you require statistics to be automatically generated via a cron job, please use the code I've provided in this post instead.

    In order for the HTML email to securely show the images produced by Awstats, it is necessary to download awstats, and upload the contents of the /wwwroot/icon folder to a publically accessible location.

    For example, i uploaded mine to http://www.mydomain.com/images/awstats/. Due to the code generated by awstats, the images must be in the /images/awstats folder.

    Next, copy & paste the code below, and edit the baseurl line at the top. Also, rename it to stats.php (or similar). If you uploaded your awstats images to http://www.yourdomain.com/images/awstats/ then the value of your baseurl would look like this:
    PHP Code:
    $baseURL 'http://www.yourdomain.com'
    Next, upload this file to your web server (eg http://www.yourdomain.com/stats.php).

    PHP Code:
    <?php 
    /*     Enter your base url below:
        eg. if you baseURL is [url]http://www.mydomain.com[/url], the awstats images are located at [url]http://www.mydomain.com/images/awstats[/url]
    */
    $baseURL 'http://www.mydomain.com';

    /*************************************************** 
    You probably don't need to edit anything below here! 
    ***************************************************/

    $printBody 0;
    if (isset(
    $_POST['submit'])) {
    // FORM HAS BEEN SUBMITTED 
        
    $username $_POST['username'];      // cpanel username
        
    $password $_POST['password'];      // cpanel password
        
    $domainName $_POST['domainName'];     // domain name to get stats for (eg mydomain.com). usually without www.
        
    $emailTo $_POST['emailTo'];         // email address to send stats to
        
    $emailFrom $_POST['emailFrom'];    // email address to send the stats from (eg MyName <me@mydomain.com)
        
        
    $hostname "http://$username:$password@$domainName:2082";
        
    $month $_POST['month'];
        
    $year $_POST['year'];
        
    $monthnames = array("""January""February""March""April""May""June""July""August""September""October""November""December");
        
    $wordMonth $monthnames[(int) $month];
        
    $emailSubject "$wordMonth $year Web Site Statistics for $domainName";
        if ((@
    $fp fopen("$hostname/awstats.pl?month=$month&year=$year&output=main&config=$domainName&lang=en&framename=mainright"'r'))) {
            
    //file opened successfully if we have reached here
            
    $i 1;
            
    $body "";
            while (!
    feof($fp)) {
                
    $chunk fgets($fp); // Read file line by line
                
                
    if ($i == 5) { // Add html code to head section
                    
    $body .= '<base href="' $baseURL '">';
                }
                if ((
    $i <= 47) || $i >= 90) {
                    
    // Save line contents
                    
    $body .= $chunk;
                }
                
    $i++;
            }
            
    fclose($fp);
            
            
    // Remove all links in the HTML file received:
            
    $body preg_replace("/<a(.*?)a>/i"""$body);
            
            
    //email contents:
            
    function sendHTMLMail($to$subject$body$from '') {
              
    $from trim($from);
              
    $rp     'ReturnNonWorkingHere@MySite.net';
              
    $org    'YourCopmany';
              
    $mailer 'YourCompany Mailer';
              
    $head  '';
              
    $head  .= "MIME-Version: 1.0\r\n";
              
    $head  .= "Content-type: text/html; charset=iso-8859-1\r\n";
              
    $head  .= "Date: "date('r'). " \r\n";
              
    $head  .= "Return-Path: $rp \r\n";
              
    $head  .= "From: $from \r\n";
              
    $head  .= "Sender: $from \r\n";
              
    $head  .= "Reply-To: $from \r\n";
              
    $head  .= "Organization: $org \r\n";
              
    $head  .= "X-Sender: $from \r\n";
              
    $head  .= "X-Priority: 3 \r\n";
              
    $head  .= "X-Mailer: $mailer \r\n";
              
    $body  str_replace("\r\n""\n"$body);
              
    $body  str_replace("\n""\r\n"$body);
              return 
    mail($to$subject$body$head);
            }
            if (isset(
    $_POST['sendEmail'])) sendHTMLMail($emailTo$emailSubject$body$emailFrom);
            echo (
    "<hr>Statistics successfully generated for $domainName for $wordMonth $year.");
            if (isset(
    $_POST['sendEmail'])) echo ("<br>Statistics emailed to $emailTo.");
            echo (
    '<hr>');
            
    $printBody 1;
        } else { 
    //Error opening file
            
    echo ("<font color=\"#FF0000\"><strong>Error opening statistics file. Please check your login details and try again.</strong></font>");
            
    $printBody 0;
        }
    }
    ?>
    <form name="stats" method="post" action="<?php echo $_SERVER['PHP_SELF'?>">
      <table  border="0" cellpadding="5">
        <tr>
          <td width="150" align="left" valign="top" nowrap><strong>cPanel Username: </strong></td>
          <td align="left"><input name="username" type="text" id="username" value="<?php if (isset($_POST['submit'])) echo $username?>" size="45" maxlength="8">        <font size="1"><br>
          (The cPanel username of the domain that you wish to generate statistics for) </font></td>
        </tr>
        <tr>
          <td width="150" align="left" valign="top" nowrap><strong>cPanel Password: </strong></td>
          <td align="left"><input name="password" type="text" id="password" value="<?php if (isset($_POST['submit'])) echo $password?>" size="45">
            <font size="1"><br>
          (The cPanel password of the domain that you wish to generate statistics for) </font></td>
        </tr>
        <tr>
          <td width="150" align="left" valign="top" nowrap><strong>Domain Name: </strong></td>
          <td align="left"><input name="domainName" type="text" id="domainName" value="<?php if (isset($_POST['submit'])) echo $domainName?>" size="45">
          <font size="1"><br>
          (Usually excluding www.) </font></td>
        </tr>
        <tr>
          <td width="150" align="left" valign="top" nowrap><strong>Email To:</strong></td>
          <td align="left"><input name="emailTo" type="text" id="emailTo" value="<?php if (isset($_POST['submit'])) echo $emailTo?>" size="45">
            <br>
          <font size="1">(Email address that the stats will be sent to. Separate multiple addresses with a comma) </font></td>
        </tr>
        <tr>
          <td width="150" align="left" valign="top" nowrap><strong>Email From: </strong></td>
          <td align="left"><input name="emailFrom" type="text" id="emailFrom" value="<?php if (isset($_POST['submit'])) echo $emailFrom?>" size="45">
          <font size="1"><br>
          (eg &nbsp;&nbsp;YourCompany &lt;sales@yourcompany.com&gt; ) </font></td>
        </tr>
        <tr>
          <td width="150" align="left" valign="top" nowrap><strong>Date (Month/Year): </strong></td>
          <td align="left"><input name="month" type="text" id="month" value="<?php if (isset($_POST['submit'])) echo $month; else echo date('m'); ?>" size="4">
    /
    <input name="year" type="text" id="year" value="<?php if (isset($_POST['submit'])) echo $year; else echo date('Y'); ?>" size="6">
    <font size="1"><br>
    (The month that the statistics should be generated from) </font></td>
        </tr>
        <tr>
          <td width="150" align="left" valign="top"><strong>Send Email?</strong> </td>
          <td align="left"><input name="sendEmail" type="checkbox" id="sendEmail" value="1"<?php if (isset($_POST['sendEmail'])) echo(' checked'); ?>>
            <br>
            <font size="1">(Tick to send the generated statistics to the email to address specified above) </font></td>
        </tr>
        <tr align="center">
          <td colspan="2"><input name="submit" type="submit" id="submit" value="Generate Statistics"></td>
        </tr>
      </table>
    </form>
    <?php 
    if ($printBody == 1) {
        echo 
    '<hr>The generated statistics are shown below:<hr>' $body; } 
    ?>
    The form should be fairly self explanitory. If you have any problems, post them in this thread.

    NB: I know this script does not generate very valid HTML code when previewing the email on screen, but this isn't too important, as long as the code in the email is valid (which it is).

    Cheers,

    James
    Last edited by james; 09-02-2004, 11:17 PM.
  • www
    Member
    • Mar 2004
    • 62

    #2
    Here is an idea, not sure if it would work but I THINK it would.

    1. Make the stats viewable from the outside world. (So you can see the stats at say www.domain.com/stats)

    2. Make a php program to pull the output of the page that you get from www.domain.com/stats and store it in a variable.

    3. Go through and convert any img tags and href links to absolute paths.

    4. Email the results to your customer in HTML format.

    5. Finally setup a cron job at a specified time to run your PHP script.

    Boy, that sounds quite nifty to make something like that…

    Comment

    • james
      Senior Member
      • Mar 2004
      • 183

      #3
      Yeh that would probably work.

      Although there's no point making the stats publically accessible - I think you can still still grab the page using php if it's protected by .htaccess.

      I think I'll end up giving this a go soon - unless someone can think of an easier way?

      Cheers,

      James

      Comment

      • Pedja
        Senior Member
        • Mar 2004
        • 329

        #4
        you have access to web stats data files. They are plain text. You ave to find out what is what within file and then you may parse it and create plain text statistic report.

        Comment

        • james
          Senior Member
          • Mar 2004
          • 183

          #5
          Ok I've had a bit of a play, writing a PHP script to do it.

          It seems to work quite well. I'm going to work on it a little more, but if anyone's interested, I'm willing to post the code once it's cleaned up a bit.

          James

          Comment

          • Frank Hagan
            Senior Member
            • Mar 2004
            • 724

            #6
            I'd be interested in that, James!

            Comment

            • Jonathan
              Senior Member
              • Mar 2004
              • 1229

              #7
              Originally posted by Frank Hagan
              I'd be interested in that, James!
              ditto
              "How can someone be so distracted yet so focused?"
              - C

              Comment

              • www
                Member
                • Mar 2004
                • 62

                #8
                Originally posted by james
                I'm willing to post the code once it's cleaned up a bit.
                That would be COOL!!!

                Comment

                • james
                  Senior Member
                  • Mar 2004
                  • 183

                  #9
                  Ok guys, I modified the script last night so that you fill in a form with the required details (cpanel username/pass, domain name, month you want stats for, etc).

                  This means that you can email stats for ANY cpanel domain to ANY email address.

                  I will most probably post this code when I get home later today/tonight.

                  James

                  Comment

                  • james
                    Senior Member
                    • Mar 2004
                    • 183

                    #10
                    For those of you waiting for my PHP script to achieve this, I've edited my initial post (above).

                    Please let me know how you go using the script!

                    James

                    Comment

                    • james
                      Senior Member
                      • Mar 2004
                      • 183

                      #11
                      The above script is not designed to run via a CRON job, for example, so use the following code instead if you wish to run this script for only one domain.

                      Edit the values of the variables at the top of the file.

                      PHP Code:
                      <?php 
                      $username 
                      'user';      // cpanel username
                      $password 'pass';      // cpanel password
                      $domainName 'yourdomain.com';     // domain name to get stats for (eg mydomain.com). usually without www.
                      $emailTo 'you@yourdomain.com';         // email address to send stats to
                      $emailFrom 'YourCompany <mail@yourcompany.com>';    // email address to send the stats from (eg MyName <me@mydomain.com)

                      $printOutput 1;     // set to 0 if you do not want the script to output anything (eg for use with a cron job)

                      /*     Enter your base url below:
                          eg. if you baseURL is [url]http://www.mydomain.com[/url], the awstats images are located at [url]http://www.mydomain.com/images/awstats[/url]
                      */
                      $baseURL 'http://www.yourdomain.com';


                      /*****************************************
                      You probably don't have to edit below here
                      *****************************************/

                      $hostname "http://$username:$password@$domainName:2082";
                      $month date('F');
                      $year date('Y');
                      $emailSubject "$month $year Web Site Statistics for $domainName";
                      $month date('m')-1;
                      if (!(@
                      $fp fopen("$hostname/awstats.pl?month=$month&year=$year&output=main&config=$domainName&lang=en&framename=mainright"'r'))) {
                          if (
                      $printOutput == 1) die("<strong>Error opening statistics file.</strong>");
                          else exit();
                      }

                      //file opened successfully if we have reached here
                      $i 1;
                      $body "";
                      while (!
                      feof($fp)) {
                          
                      $chunk fgets($fp); // Read file line by line
                          
                          
                      if ($i == 5) { // Add html code to head section
                              
                      $body .= '<base href="' $baseURL '">';
                          }
                          if ((
                      $i <= 47) || $i >= 90) {
                              
                      // Save line contents
                              
                      $body .= $chunk;
                          }
                          
                      $i++;
                      }
                      fclose($fp);

                      // Remove all links in the HTML file received:
                      $body preg_replace("/<a(.*?)a>/i"""$body);

                      //email contents:
                      function sendHTMLMail($to$subject$body$from '') {
                        
                      $from trim($from);
                        
                      $rp     'ReturnNonWorkingHere@MySite.net';
                        
                      $org    'YourCopmany';
                        
                      $mailer 'YourCompany Mailer';
                        
                      $head  '';
                        
                      $head  .= "MIME-Version: 1.0\r\n";
                        
                      $head  .= "Content-type: text/html; charset=iso-8859-1\r\n";
                        
                      $head  .= "Date: "date('r'). " \r\n";
                        
                      $head  .= "Return-Path: $rp \r\n";
                        
                      $head  .= "From: $from \r\n";
                        
                      $head  .= "Sender: $from \r\n";
                        
                      $head  .= "Reply-To: $from \r\n";
                        
                      $head  .= "Organization: $org \r\n";
                        
                      $head  .= "X-Sender: $from \r\n";
                        
                      $head  .= "X-Priority: 3 \r\n";
                        
                      $head  .= "X-Mailer: $mailer \r\n";
                        
                      $body  str_replace("\r\n""\n"$body);
                        
                      $body  str_replace("\n""\r\n"$body);
                        return 
                      mail($to$subject$body$head);
                      }
                      sendHTMLMail($emailTo$emailSubject$body$emailFrom);
                      if (
                      $printOutput == 1) {
                          echo (
                      "<hr>Statistics successfully generated for $domainName for $month/$year.<br><br>Email sent to $emailTo.<br>A copy of the email contents is included below:<hr>");
                          echo 
                      $body;
                      }
                      ?>
                      If running via a cron job, be sure to set the printOutput variable to 0 (no).

                      In order for the HTML email to securely show the images produced by Awstats, it is necessary to download awstats, and upload the contents of the /wwwroot/icon folder to a publically accessible location.

                      For example, i uploaded mine to http://www.mydomain.com/images/awstats/. Due to the code generated by awstats, the images must be in the /images/awstats folder.

                      Next, copy & paste the code below, and edit the baseurl line at the top. Also, rename it to stats.php (or similar).
                      Cheers,

                      James
                      Last edited by james; 09-02-2004, 11:51 PM.

                      Comment

                      Working...