Setup SSMTP on Ubuntu servers with crontab
Learn how to set up the standalone job on Linux using Crontab and SSMTP to sent status out to you
First of all, you need a Linux machine to make this happen! On most of Linux distros crontab is available by default, specifically on servers. But anyway we will start this tutorial from scratch assuming you don’t even have crontab available.
Installing Crontab
apt-get update && apt-get upgrade
Install cron
apt-get install cron
systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-07-02 20:38:53 IST; 4 days ago
Docs: man:cron(8)
Main PID: 978 (cron)
Tasks: 1 (limit: 4915)
Memory: 2.0M
CGroup: /system.slice/cron.service
└─978 /usr/sbin/cron -f
this shows your crontab is working fine.
Now let's try editing the crontab, use root *if you need any *sudo to be used in your script. Depending on your need you can use *crontab -e *for different users on the system and every user has its own crontab installed.
crontab -e
(choose nano or any other editor of your choice, if asked)Adding below contents at the end of the file
[MAILTO=yourmail@gmail.com](mailto:MAILTO=mybuddypro@gmail.com)
* */12 * * * bash /home/rocking-shell/mongo-rolling-backup.sh
Save above file, and crontab will say, installing crontab
This means your crontab is setup now, more details here
Installing SSMTP
apt install ssmtp
Now edit SMTP configurations here
sudo vi /etc/ssmtp/ssmtp.conf
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
[root=defaultEmail@gmail.com](mailto:root=er.mr.prasad@gmail.com)# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587
[AuthUser=youremail@gmail.com](mailto:AuthUser=er.mr.prasad@gmail.com)
AuthPass=yourpassword
UseSTARTTLS=yes# Where will the mail seem to come from?
#rewriteDomain=# The full hostname
#hostname=prasad-HP-Notebook# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=NO
Make sure when you use Gmail, you need to enable less secure apps in settings, you can check this here
Let's try testing the ssmtp by sending an email
nano mail-sample.txtenter below contents to itTo: recipient_email@mydomain.com
From: youremail@gmail.com
Subject: test email from ssmtp!now run ssmtp**ssmtp to_email@gmail.com < mail-sample.txt**
Yaayy! You are done. Please let me know in the comments if you face any issues or need any help.