How to Configure Postfix with SMTP for Cron Job Email Alerts on Ubuntu Server


1 views

When setting up email notifications from cron jobs on Ubuntu, you'll need a Mail Transfer Agent (MTA). Postfix is the most widely used MTA on Linux systems due to its reliability and straightforward configuration.

sudo apt update
sudo apt install postfix mailutils

During installation, select "Internet Site" when prompted, then enter your domain name if you have one, or your server's hostname.

Edit the main configuration file:

sudo nano /etc/postfix/main.cf

Ensure these key parameters are set:

myhostname = your-server-hostname
mydomain = your-domain.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_protocols = all

Verify the installation works before configuring external SMTP:

echo "Test email body" | mail -s "Test Subject" your@email.com

Check the mail queue:

mailq

For reliable delivery (especially when sending to external domains), configure SMTP relay through a service like Gmail or SendGrid:

sudo nano /etc/postfix/sasl_passwd

Add your SMTP credentials:

[smtp.gmail.com]:587 username@gmail.com:password

Secure the credentials file and update Postfix:

sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

Update main.cf with your SMTP settings:

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Edit your crontab to specify email recipient:

crontab -e

Add this line at the top:

MAILTO="your@email.com"

Example cron job that emails output:

0 * * * * /path/to/script.sh > /dev/null 2>&1 | mail -s "Cron Job Report" your@email.com

Check logs for email delivery problems:

tail -f /var/log/mail.log

Test SMTP connectivity:

telnet smtp.gmail.com 587

Verify DNS settings if emails aren't delivering:

dig mx your-domain.com

For simpler setups, consider:

sudo apt install ssmtp

Or for cloud-based solutions:

curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN/messages \
    -F from='Server Alerts <alerts@yourdomain.com>' \
    -F to=you@example.com \
    -F subject='Cron Job Notification' \
    -F text='Your cron job has completed'

When setting up email delivery from Ubuntu's cronjobs, you'll need a Mail Transfer Agent (MTA). Postfix is the most widely used MTA in Linux environments due to its reliability and straightforward configuration.

sudo apt update
sudo apt install postfix mailutils

During installation, you'll be prompted to select configuration type. Choose "Internet Site" and enter your domain name when requested.

sudo nano /etc/postfix/main.cf

Key parameters to verify or modify:

myhostname = your.server.hostname
mydomain = yourdomain.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
relayhost = [smtp.yourprovider.com]:587

For external SMTP servers (like Gmail or SendGrid):

sudo nano /etc/postfix/sasl/sasl_passwd

Add your SMTP credentials:

[smtp.yourprovider.com]:587 username:password

Then secure and apply the credentials:

sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

Send a test email from command line:

echo "Test message" | mail -s "Test Subject" recipient@example.com

Check mail logs for troubleshooting:

tail -f /var/log/mail.log

To have cron send output via email:

0 3 * * * /path/to/script.sh | mail -s "Cronjob Report" admin@yourdomain.com

For simpler setups requiring only outgoing mail:

sudo apt install ssmtp
sudo nano /etc/ssmtp/ssmtp.conf

Sample configuration:

root=your@email.com
mailhub=smtp.gmail.com:587
AuthUser=your@gmail.com
AuthPass=yourpassword
UseSTARTTLS=YES
  • Port 25 blocked by many ISPs - use alternative ports (587, 465)
  • Check spam folders for test emails
  • Verify DNS MX records if using your own domain
  • Review /var/log/mail.err for error details