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:
Next, upload this file to your web server (eg http://www.yourdomain.com/stats.php).
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
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';
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 YourCompany <sales@yourcompany.com> ) </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; }
?>
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
Comment