Automating Email Reporting

You may have to report your daily (or weekly, monthly) progress to your supervisor. It is a good practice in multiple ways: you know what you did in a day and thus, keep track of your progress related to your work, you create a task completion history which you can go back and check anytime, and it keeps you motivated since your supervisor is looking at your progress. :)

However, everyday you need to send an email in a certain way (for example, email subject in a specific format). Many times, even though you have worked during the day, you couldn't send the email in the evening due to various reasons (for instance, you are tired, attending a party, or attending an evening technical event (meetup)).

We can simplify and separate the task in two sub-tasks

  • Writing text describing the progress
  • Sending the email

Whenever you complete a task, you make an entry in a text file and the rest can be automated to make our lives easier. In this post, I am describing steps to automate the process of sending such periodic emails. I executed these steps on my Macbook Pro running OS X 10.11.4.

Configure Postfix

Open terminal and edit configuration file of postfix.

sudo vi /etc/postfix/main.cf

Go to the end of the file, and add the following lines:

# Gmail SMTP 
relayhost=smtp.gmail.com:587 
# SASL authentication in the Postfix SMTP client. 
smtp_sasl_auth_enable=yes 
smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd 
smtp_sasl_security_options=noanonymous 
smtp_sasl_mechanism_filter=plain 
# Enable Transport Layer Security (TLS) 
smtp_use_tls=yes 
smtp_tls_security_level=encrypt 
tls_random_source=dev:/dev/urandom

Create the sasl_passwd file

Create the sasl_passwd file with the SMTP credentials

sudo vi /etc/postfix/sasl_passwd

Write the following line in the file:

smtp.gmail.com:587 your_email@gmail.com:your_password

We also need to create the Postfix lookup table (sasl_passwd.db) from the sasl_passwd file.

sudo postmap /etc/postfix/sasl_passwd

Restart Postfix

sudo postfix reload

Check hostname

Check whether your hostname has been set properly to avoid the error "fatal: unable to use my own hostname". Type the command "hostname" and see whether you see an IP address or a non-numeric human readable name. If you see an IP address in the response to the "hostname" command, you may change the hostname by the following command:

sudo scutil --set HostName newName

For Gmail, turn on less secure apps

If you are using Gmail, you need to allow less secure apps to access Gmail to avoid authentication failures. You may change the settings here to allow less secure apps.

Test the mail

If everything has been properly configured, the following command should send an email to your desired account.

date | mail -s testing your_email@gmail.com

Setup a Git repo

After setting up the email mechanism, you may opt to setup a git repo to commit everyday's progress.

mkdir progress 
cd progress 
git init

Write a script to customize the email

Sometimes, you may not have anything to report. In those days, your system should not send an email. This can be achieved by writing the progress everyday in a pre-defined text file. You write a script to commit the changes in the file to the git repo, send the contents of the file as the email body only if you have something written in it, send the email, and finally clear the contents of the file.

I wrote the following script to achieve the above mentioned tasks.

#!/bin/sh 
cd /Path/to/progress 
git commit -m "new progress report" -a 
if [ -s progress.txt ] then   
mail -s "[Progress report Tushar] $(date +%F)" supervisorEmail <progress.txt 
fi 
# Clear the file, since we have sent it already and commited it in the repo 
> progress.txt

You may need to customize the subject based on your requirements and obviously replace the "supervisorEmail" with an appropriate email (to whom you wish to send the email).

You also need to add the script and the progress.txt to the git repo.

git add *

Schedule emails using Cron

Now, the last part of the puzzle is to write a cronjob that will execute the above script periodically. You can add/edit your cronjobs by the following command:

crontab -e

You specify the cronjob based on your requirements. The following job runs 11 PM everyday.

0 23 * * * /path/to/progress.sh

That's it. Everyday, whatever you write in progress.txt will be sent to your supervisor (if it is not empty). After sending the email, the scripts clears the contents of the file so that next day you start fresh.

[Thanks to Prof. Spinellis for optimizing the scrip]