Skip to content

Instantly share code, notes, and snippets.

@Justintime50
Last active October 26, 2021 06:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Justintime50/6053e4657dd6d9ccec6cda20ec5389a5 to your computer and use it in GitHub Desktop.
Save Justintime50/6053e4657dd6d9ccec6cda20ec5389a5 to your computer and use it in GitHub Desktop.
Setup a Mail Server on macOS - No Dependencies Required!

Setup a Mail Server on macOS

After years of trying to setup a mail server on macOS without requiring dependencies or messing with ports to trick my ISP into letting mail out, I figured out a solution.

Note: This guide is opinionated towards a Gmail setup.

Steps

  1. Backup the existing Postfix configuration:
sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.bak
  1. Configure the Postfix mail server:
sudo emacs /etc/postfix/main.cf

# Paste the following under the commented out `relayhost` settings
relayhost = smtp.gmail.com:587
smtp_sasl_mechanism_filter = plain
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
# smtp_tls_security_level = encrypt
# tls_random_source = dev:/dev/urandom
  1. Add Gmail username and password to Postfix:
sudo emacs /etc/postfix/sasl_passwd

# Paste the following into the file
# If using 2FA, you'll need an "app password" here: https://security.google.com/settings/security/apppasswords
smtp.gmail.com:587 EMAIL:PASSWORD
  1. Generate the hash database and secure the file:
sudo postmap /etc/postfix/sasl_passwd
sudo chown root: /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
  1. Restart the Postfix mail server:
sudo launchctl stop org.postfix.master
sudo launchctl start org.postfix.master

Send an Email from the CLI

echo "You are ready to send email from the command-line" | mail -s "Congratulations!" example@gmail.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment