SEARCH

How-To Geek

How to Automatically Generate and Email Computer Statistics

It’s important to check various log data and statistics on your server every day, but it grows to be tedious.  Wouldn’t it be nice to receive a single email with all the highlights each day, so you don’t even need to get on the server to check for problems?  This guide will show you how to configure automated emails on Linux and Windows.

We will be specifically covering this for Ubuntu and Windows 8.1, with Gmail being used as the email server that mail is sent from.  If you have another version of Linux or Windows, or prefer to use some other email service, the instructions here should be easily adaptable.

Automated Emails in Linux

We’re going to use two different packages to accomplish this, ssmtp and mailutils, so install both of them with the following command:

$ sudo apt-get install ssmtp mailutils

Once those are installed, we need to make some changes to the SSMTP configuration file:

$ sudo vi /etc/ssmtp/ssmtp.conf

Edit the file with these changes (it’s safe to just erase all the text in the file and copy/paste these settings, if you’d like):

# This address will receive the emails, so enter your own email here if you want to receive them.

root=username@gmail.com

# Specify the email server here (leave as is if you’re using Gmail).

mailhub=smtp.gmail.com:587

# The domain name that the mail will come from.

rewriteDomain=gmail.com

# The email address that these emails should be from.

hostname=username@gmail.com

# SSL/TLS settings, required for Gmail and most other mail servers.

UseTLS=Yes

UseSTARTTLS=Yes

# The username and password to your Gmail account.

AuthUser=username

AuthPass=password

# Allow the ability to specify a from address different than the one above.

FromLineOverride=yes

When you’re done editing the file, you’ll want to change the permissions since your Gmail password is stored in plaintext.

$ sudo chmod 640 /etc/ssmtp/ssmtp.conf

$ sudo chown username.username /etc/ssmtp/ssmtp.conf

It’s always more secure to make root the owner of the file, but then we would have to use the sudo command in our script and it would prompt us for a password, thereby defeating the purpose of automating this whole process.

If you’re using a shared server and are worried about your password being stored in plaintext and readable by root, create a throwaway Gmail account or use an email server that doesn’t require any type of authentication in the first place.

To make sure everything is configured correctly, let’s try a test email:

$ echo "Testing" | mail -s "Testing mail setup" username@gmail.com

“Testing” will be in the body of the email and the subject will be “Testing mail setup.”  Check your email to make sure you received it.

Writing a Script for the Emails

Now that we’re able to send emails from the command line, let’s write a script that will send us some basic information about our system.

#!/bin/bash

# Check hard drive space

echo "Hard drive space:" > /home/geek/email.txt

df -h >> /home/geek/email.txt

# List the users that are logged in

echo "Users currently logged in:" >> /home/geek/email.txt

who >> /home/geek/email.txt

# List currently running processes

echo "Running processes:" >> /home/geek/email.txt

ps -e >> /home/geek/email.txt

# Send the email

cat /home/geek/email.txt | mail -s "Daily server information" username@gmail.com

# Delete the file we created

rm /home/geek/email.txt

Obviously you can get a lot more in-depth with your script and even make the formatting a little nicer, but this is what the output looks like in our email:

Now that the script is written and tested, we can use cron to automatically execute it at the same time every day.  In this example, we will configure the email to be sent at 2:00 AM every morning, so we can go through the data later that day.

$ crontab -e

For 2:00 AM emails, add:

0 2 * * * /home/geek/script.sh

We’ve written an entire article on crontab files if you need more help with this part.

Automated Emails in Windows

Sending emails at the command line is possible through PowerShell, but we’ve found that implementing this functionality is a lot easier with third party apps, especially when using Gmail.  SendEmail is a free program available for Windows that makes integration with Windows Task Scheduler and Gmail a breeze.  Click the link above to download the latest copy, and make sure you grab the TLS-supported version.

Once you’ve downloaded SendEmail, extract the zip file and put the contents somewhere that you can store them for as long as you plan to send automated emails.  In this example, we’re just going to store the program in C:\SendEmail

Let’s test out SendEmail to get a quick feel for how it works.  Open a command prompt by typing cmd into the Start or Run (Ctrl+R) menu.

With the command prompt open, use the change directory command to navigate to where you stored the SendEmail files.

cd C:\SendEmail

Now we can try sending a test email with the following command:

sendEmail -f username@gmail.com -t username@gmail.com -s smtp.gmail.com:587 -xu username -xp password -u "Test email subject" -m "This is a test email."

Obviously, replace “username” and “password” with your account credentials before executing the command.

Here’s what the command above actually does:

sendEmail executes the program.

-f – from address

-t – to address

-s – SMTP server

-xu – account username

-xp – account password

-u – email subject

-m – email body text

Check your inbox to make sure you received the test email, and then we can move on to writing a script that will send us server information.

Writing a Script for the Emails

To get the most out of our script, we’re going to write it for PowerShell.  Open up Windows PowerShell ISE by typing powershell_ise.exe into a Run prompt (Ctrl+R).

On the right side of the PowerShell ISE window, you can list every command that PowerShell is capable of executing.  This should give you a good start on generating the types of information that you need reported.  In your script, you can also call on third party programs to output information as well (i.e. SendEmail is a third party app but PowerShell and cmd can use it to accomplish tasks they normally can’t).

For our example script, we will check the current disk usage of the C drive, show the currently running processes, and show all files that are currently being shared out over the network.

# Check hard drive space

echo "C: Drive Usage:" > C:\SendEmail\info.txt

Get-WmiObject win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace >> C:\SendEmail\info.txt

# List currently running processes

echo "Running processes:" >> C:\SendEmail\info.txt

get-process >> C:\SendEmail\info.txt

# List the files/folders currently being shared

echo "SMB shares:" >> C:\SendEmail\info.txt

get-smbshare >> C:\SendEmail\info.txt

# Send the email

type C:\SendEmail\info.txt | C:\SendEmail\sendEmail -f username@gmail.com -t username@gmail.com -s smtp.gmail.com:587 -xu username -xp password -u "Daily server info"

# Delete the file we made

rm C:\SendEmail\info.txt

In this script, various information is outputted to C:\SendEmail\info.txt, and then the text in that document is emailed to us before it gets deleted.  Save your script with a ps1 file extension (PowerShell file).

With your script finished, run a quick test from a Run prompt to make sure it’s working properly.

Just use the powershell command with a -file argument and specify the path to your script.

powershell -file "c:\SendEmail\daily-email.ps1"

Check your inbox to make sure you received the email – if not, look over your script for syntax errors.  Here’s what the generated email from our example script looks like:

You can play around with the formatting (such as echoing empty lines between text) to make it display nicer on your device, or better yet you can use a third party program that will output the needed info in a more readable format than Windows does (the scripting process will still be the same).

Once you’ve worked out the kinks in your script, you can use Windows Task Scheduler to automate it.  Open Windows Task Scheduler via the Start menu.

With the Task Scheduler open, select Action > Create Basic Task.

Name this task something like “Daily email script” and click next.  On the next screen, select the frequency with which you’d like your email script to run, probably daily.  Then, choose the time you’d like the script to run, and hit next.

Now you should be on the “Action” part of the wizard, select “Start a Program” and enter the same text that we entered into the Run prompt earlier to test our script.

Hit next and then hit Yes on this window:

Click Finish on the last menu, and you’re done scheduling your automatic emails.

Korbin Brown is an IT enthusiast with a passion for writing. He enjoys troubleshooting complex Windows, Linux, and networking issues and sharing his experiences with fellow geeks.

  • Published 01/23/14

Enter Your Email Here to Get Access for Free:

Go check your email!