This guide will help configure MacOS so it can send emails from the command-line (Terminal) and shell scripts via an SMTP server. This is useful for enabling scripts, scheduled jobs, etc. to send email notifications.
The example in this post work for Rackspace’s SMTP server. The guide is adaptable to other SMTP servers and email providers, while acknowledging that you may need to adjust certain details. Email providers can vary in how they require users to authenticate and interface with their SMTP servers.
There are lot of examples online for using gmail’s SMTP servers, but since I couldn’t find any complete examples for Rackspace, I decided to write this post!
Configuring postfix on MacOS
MacOS/OSX comes bundled with the postfix mail server. We will configure that to use Rackspace’s SMTP server to send mail.
To use Rackspace’s SMTP, its a given that you must be a Rackspace customer. Create an email address that you wish to be the “sender” of any notifications. Note this email account’s credentials as you’ll need to include them in your postfix configuration.
I recommend that you use a dedicated email address for sending automated notifications (e.g. no-reply@.., notify@.., etc), rather than an email address with an inbox that is important to your business. Within Rackspace’s control panel, you have the option to configure an auto-responder for your automated notification address so that anyone that replies to a notification or sends anything else to it will receive a canned reply in response.
Step1: Edit main postfix configuration file
Make a copy of your original postfix conf file:
sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.bak
Open postfix’s config file for editing:
sudo nano /etc/postfix/main.cf
In an untouched postfix configuration on MacOS, the following 3x separate lines have the following values:
mydomain_fallback = localhost
mail_owner = _postfix
setgid_group = _postdrop.
If you have previously edited your postfix settings, you may wish to search for each item and reset these back to their default values (first confirm that you aren’t about to break anything!).
Add the following to the very end of the open postfix config file:
relayhost = smtp.emailsrvr.com:587 smtp_sasl_auth_enable=yes smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd smtp_sasl_mechanism_filter = AUTH LOGIN smtp_sasl_security_options = smtp_use_tls=yes smtp_tls_security_level=encrypt
These setting are specific to Rackspace’s requirements. If you are using another SMTP server, you will need to specify different values.
Save and exit the editor.
Step 2: create the sasl_passwd file for postfix to use
Create the /etc/postfix/sasl_passwd file referenced in main.cf with the following line of text, substituting the email@example.com and password placeholders with your own:
Next, use the
postmap tool to create a lookup table from the sasl_passwd file:
sudo postmap /etc/postfix/sasl_passwd
This will create the file sasl_passwd.db
Step 3: Restart postfix
sudo postfix reload
You might see some warnings, but they should be benign. We don’t need a full mail system, just the ability to send smtp emails. Move on to the next step to test your configuration!
Step 4: Send a test email
The following command sends a test email to firstname.lastname@example.org. Give it a shot by substituting your email address:
echo "testing testing test email" | mail -s test email@example.com
Note this command does not produce any output when it runs successfully.
Next, check for the test email in your inbox (if its not there, be sure to also check the spam folder). You may want to add your notification email address to your recipient inbox’s address book to ensure better deliverability.
If you need to troubleshoot, it can be helpful to check the mail queue:
You can clear all queued emails with the following command:
sudo postsuper -d ALL
On OSX, postfix logged to the /var/log/mail.log file. On MacOS, this log file doesn’t exist.
For troubleshooting with MacOS, you can view the log in real-time with the command:
sudo log stream --predicate '(process == "smtpd") || (process == "smtp")' --info
You could have this running in one Terminal tab, and then try to send a test email in another tab, to see what messages you receive.
Thanks to this article from <developerfiles.com> which has instructions for using gmail, as well as some commenters on various forums that provided tips to adapt SMTP settings for Rackspace. Rackspace’s documentation was also helpful in realizing a working configuration.