Originally written spring of 2014
Ever need statistics, notifications, or even just server log files emailed to you? This tutorial is for you.
Setting up simple script to have your server automatically email you information every so often is quite simple, just follow these steps. Please note, this tutorial will be for Debian Linux. For CentOS and other linux distributions it shouldn't be too different, but this article only covers debian.
Step 1:You'll need to install MSMTP, a command line SMTP mail client. This can be done by running:
sudo apt-get install msmtp
from command line. Once that installs, we can move on to the next step.
Step 2:Now we need to setup the config file for MSMTP. You'll have to create a config file for it in your home directory. To do this, you'll need to create and edit a file named
.msmtprc, which can be done with your favorite text editor. I'll post my example config here:
account default tls_starttls on auth login host mail.yourdomain.com port 587 user firstname.lastname@example.org from email@example.com password your-password tls on tls_certcheck off
Your config file may differ depending on your mail provider, for more information please contact your mail provider.
Now, we need to change permissions of that config file. Make sure your in your home directory, and change the file permissions of .msmtprc to 600. On Debian this can be done by typing:
chmod 600 .msmtprc
And now we need to test MSMTP, which can easily be done by typing
echo 'Test-Text' | msmtp -a default firstname.lastname@example.org
You should receive a message to "email@example.com" that says "Test-Text". If you got that email, your ready to continue on. If you didn't please re-read above, or leave a comment and I will help you.
Step 3: Automate the process. Because automation is fun. We'll be creating some simple scripts and setting up some cron jobs, so heres where your linux knowledge will come in handy.
This part is up to you, you'll need to figure out what script you want to use. In this case, I'll be using a script that gives me system information.
After setting up the script and making it run, its time to setup the cron job. In my case, I'll be having my server email me usage stats every week, on a monday morning at 6am. My cronjob will look like this:
15 6 * * 1 /home/justin/usage.sh | msmtp -a default firstname.lastname@example.org
Since cronjobs can be confusing, you can use this handy site to create cron jobs. You can customize this to your hearts content, or feel free to use what I have posted.