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

Vadim KononovVadim Kononov
2 min read

Looking to automate email notifications from your macOS using CRON jobs? This guide will walk you through configuring Postfix on a 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 (we'll use Gmail as an example)

Step 1: Verify Postfix Installation

Check if Postfix is installed on your macOS:

postconf -d | grep mail_version

You should see output similar to this:

mail_version = 3.2.2
milter_macro_v = $mail_name $mail_version

Step 2: Configure Postfix

  1. Edit the main configuration file:

    Open /etc/postfix/main.cf with superuser privileges:

     sudo vi /etc/postfix/main.cf
    

    Configure the following settings:

    • Set up your SMTP server and port. For Gmail, use port 465:

        relayhost = [smtp.gmail.com]:465
      
    • Enable SASL authentication:

        smtp_sasl_auth_enable = yes
      
    • Specify the path to the SASL password file:

        smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      
    • Set security options:

        smtp_sasl_security_options = noanonymous
      
    • Enable TLS and set the security level:

        smtp_use_tls = yes
        smtp_tls_security_level = encrypt
        smtp_tls_note_starttls_offer = yes
      
    • Enable TLS wrapper mode:

        smtp_tls_wrappermode = yes
      
    • Define your domain:

        mydomain = example.com
        myorigin = $mydomain
      

Step 3: Create the SASL Password File

  1. Create the sasl_passwd file:

     sudo vi /etc/postfix/sasl_passwd
    

    Add your SMTP credentials:

     [smtp.gmail.com]:465 your_email@gmail.com:your_password
    

    Regardless of the email address you enter CRON always sends emails from username@domain.tld, where username is the user under which CRON is running.

  2. Secure and hash the password file:

     sudo postmap /etc/postfix/sasl_passwd
     sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
    
  3. Reload Postfix to apply the changes:

     sudo postfix reload
    

    If you get this error:

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

    then you need start Postfix:

     sudo postfix start
    

Step 4: Testing

You can test your CRON jobs with something like this:

* * * * * echo "Hello, this is a test from CRON" | sendmail recipient@example.com
* * * * * bash /path/to/invalid/script

Replace recipient@example.com with the email address where you want to send the output.

Troubleshooting

  • If emails aren't being received, check the Postfix log for errors:

      log stream --predicate  '(process == "smtpd") || (process == "smtp")' --info --debug
    
  • Verify the sender email is authorized to send through the SMTP server.

Conclusion

You've now configured your macOS to send automated emails from CRON jobs using Postfix, enhancing your productivity and monitoring capabilities.

0
Subscribe to my newsletter

Read articles from Vadim Kononov directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vadim Kononov
Vadim Kononov

I am an accomplished Solution Architect, Full Stack Developer and DevOps Specialist with a passion for creative leadership and mentorship, business optimization and technical direction, and ingenious solutions to complex problems. I am especially interested in App & Web Development, Cyber Security, Cloud Computing, Data Science, Open Source Software, Statistical Analysis and Discrete Mathematics.