Skip to content

How to send Awstats as an Email

Versión en español de esta publicación.
Some months ago I installed Awstats in my home server (but that’s not covered in this post). The problem I had is that I barely check the URLs to see my sites statistics because is hard to remember to do so.

I’m the kind of guy that puts more attention to email when it comes to data or system alerts. So I decided that, to send my site statistics to my email instead of having to use the password protected url to see them. I started to search a plugin or a built-in functionality to do this in the simplest way possible in order to not spend time getting this to work. Maybe I did not search enough because I didn’t found a way to do it automatically.

So I started to look at some options to accomplish sending my site stats to the email. Here is the approach that I came up with. Maybe it can be useful to someone.

function send_awstats() {
    # Download awstats for all sites. Send them as html email 
    for ((i = 0; i < ${#__server_sites[@]}; i++)); do
        wget --tries=3 --output-document=/tmp/${__server_sites[$i]}.html --no-check-certificate --user user --password pass https://awstats/url/?config=${__server_sites[$i]}
        mail -a "MIME-Version: 1.0" -a "Content-Type: text/html" -s "Daily Awstats [${__now}]: ${__server_sites_domains[$i]}" root@localhost < /tmp/${__server_sites[$i]}.html
        rm -f /tmp/${__server_sites[$i]}.html
    done
}

Basically what this script does is that it makes a request to the Awstats url and saves the content as an html file in a temporal directory. After that it sends the saved file as an html email. In order for this to work you have to have Awstats installed and also postfix for sending emails.

This script can be placed in a .sh file and setup a cron job to periodically and according to your needs send the email with the latest site statics data.

Enjoy!
-Yohan

Leave a Reply

Your email address will not be published. Required fields are marked *