Periodic Email from Qnap with Sendmail and Crond

Qnap offers Auto Email notification , but they are limited to ‘Errors and Warnings’. So you can’t do for instance – A Disk health check every Saturday and email you the result.  You only get the notification on teh Errors and Warnings with the GUI front.

In our case, I wanted to have the listing of files in a particular Directory each day emailed to me , just to ensure that the backup is working as expected.

Emailing from Qnap can be very easily acheived, offcourse you will need

  1. Putty on Windows, or a way to SSH into your Qnap
  2. Your Qnaps admin username and password
  3. basic knowledge of working with VI, VIM or nano editor.
  4. the format of crontab file .

Step 1. Configure the SMTP server 

Go to System settings > notifications>emails and enter your SMTP server details. Don’t use gmail accounts as Gmail uses Oauth protocol that works with the front end, but fails with Sendmail.

Enter your SMTP server details and test it.
Enter your SMTP server details and test it.

Step 2.  Test your Script with sendmail

My Script simply lists the contents of a directory and dumps it to a mail file, which will later be sent using sendmail.

> email.txt
echo To: > email.txt
echo From: >> email.txt
echo Subject: Quickbooks Backup Status >> email.txt
echo "===============Body of the Email===============" >> email.txt
ls /share/DR/QBBACKUP/ >> email.txt
cat email.txt | sendmail -v -t

First it clears the content of email.txt, then creates the header of the email message with To, from and Subject . Use the same from email address which you have specified in step 1 in SMTP server, to ensure that it doesn’t end in spam.

Finally it Dumps the output of  list command to the file.  you will have your command here from which you need the output.

The last line dumps the content of email.txt to sendmail, which scans it for recipient address ( -t option) and shows you what’s happening ( -v option).

make the file executable and run it to see if it works.

Step 3 : Edit Crontab to schedule it

Don’t use the crontab -e to edit the crontab. Instead edit your crontab file directly which resides on /etc/config/crontab. After edit, tell crontab to read the config file again, and then restart the cron deamon.

crontab /etc/config/crontab
/etc/init.d/ restart

If everything goes well, you will start getting the emails as per the schedule you have setup in crontab