VK
Vadim Kononov

Solutions Architect

Set Up macOS to Use Postfix for Sending Automated Emails from CRON

Set Up macOS to Use Postfix for Sending Automated Emails from CRON

📅
macospostfixcron

Looking to automate email notifications from your macOS using CRON jobs? This guide walks through configuring Postfix on macOS to send emails from CRON jobs to a designated email address.

Prerequisites

  • Access to a macOS computer

  • Basic knowledge of terminal commands

  • An email account with SMTP access (Gmail used as an example)

Step 1: Verify Postfix Installation

Check whether Postfix is installed:

postconf -d | grep mail_version

Expected output:

mail_version = 3.2.2
milter_macro_v = $mail_name $mail_version

Step 2: Configure Postfix

Edit the main configuration file

Open the Postfix configuration file with superuser privileges:

sudo vi /etc/postfix/main.cf

Add or update the following settings.

SMTP relay configuration (Gmail example)

relayhost = [smtp.gmail.com]:465

Domain configuration

mydomain = example.com
myorigin = $mydomain

Enable SASL authentication

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

TLS configuration

smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_note_starttls_offer = yes
smtp_tls_wrappermode = yes

Step 3: Create the SASL Password File

1. Create the credentials file

sudo vi /etc/postfix/sasl_passwd

Add your SMTP credentials:

[smtp.gmail.com]:465 [email protected]:your_password

CRON always sends mail as [email protected], where username is the user under which the CRON job runs, regardless of the email specified here.

2. Secure and hash the file

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

3. Reload or start Postfix

Reload Postfix:

sudo postfix reload

If Postfix is not running:

postfix/postfix-script: fatal: the Postfix mail system is not running

Start it manually:

sudo postfix start

Step 4: Testing

Example CRON entries:

* * * * * echo "Hello, this is a test from CRON" | sendmail [email protected]
* * * * * bash /path/to/invalid/script

Replace [email protected] with the destination address.

Troubleshooting

  • Inspect Postfix logs for errors:
log stream --predicate '(process == "smtpd") || (process == "smtp")' --info --debug
  • Ensure the sender account is authorized to relay through the SMTP server.

Conclusion

Postfix is now configured to deliver CRON output via email on macOS, enabling automated monitoring and alerting.