Skip to content

Instantly share code, notes, and snippets.

@mohd-akram
Last active May 13, 2024 16:58
Show Gist options
  • Save mohd-akram/4584d9d9d340d0fba99d4a7d0e823f02 to your computer and use it in GitHub Desktop.
Save mohd-akram/4584d9d9d340d0fba99d4a7d0e823f02 to your computer and use it in GitHub Desktop.
Create a local Postfix instance
#!/bin/sh
set -eux
# Enable multi-instance operation
sudo postmulti -e init
mkdir -p ~/.config ~/.local/state/spool
# Create a new local instance
sudo postmulti -I postfix-$USER -e create \
config_directory=$HOME/.config/postfix \
queue_directory=$HOME/.local/state/spool/postfix \
data_directory=$HOME/.local/state/postfix
# Set permissions to our user
sudo postmulti -p -i postfix-$USER set-permissions mail_owner=$USER
# Enable the instance
sudo postmulti -e enable -i postfix-$USER
# Enable sendmail, SMTP and SASL
sudo postmulti -x -i postfix-$USER postconf \
authorized_submit_users=$USER \
smtp_tls_security_level=secure \
smtp_sender_dependent_authentication=yes \
smtp_sasl_auth_enable=yes \
smtp_sasl_tls_security_options=noanonymous \
smtp_sasl_password_maps=texthash:$HOME/.config/postfix/sasl_passwd \
smtp_generic_maps=texthash:$HOME/.config/postfix/generic \
sender_dependent_relayhost_maps=texthash:$HOME/.config/postfix/sender_relay
# Add SMTP credentials
# https://www.postfix.org/SOHO_README.html#client_sasl_sender
# https://gist.github.com/mohd-akram/eb3dc7fe6313522c42c4a875297005fc
# Run `sudo chmod 600 sasl_passwd` after to restrict permissions
# Add `export MAIL_CONFIG=$HOME/.config/postfix` to your
# shell profile to use this instance for sending
# For macOS, enable autospawn when mail is available
if [ -d /Library/LaunchDaemons ]; then
# Same as /System/Library/LaunchDaemons/com.apple.postfix.master.plist
# but using our configuration and queue directories
sudo tee /Library/LaunchDaemons/org.postfix-$USER.master.plist >/dev/null <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.postfix-$USER.master</string>
<key>Program</key>
<string>/usr/libexec/postfix/master</string>
<key>ProgramArguments</key>
<array>
<string>master</string>
<string>-c</string>
<string>$HOME/.config/postfix</string>
<string>-e</string>
<string>60</string>
</array>
<key>QueueDirectories</key>
<array>
<string>$HOME/.local/state/spool/postfix/maildrop</string>
</array>
<key>AbandonProcessGroup</key>
<true/>
</dict>
</plist>
EOF
sudo launchctl load /Library/LaunchDaemons/org.postfix-$USER.master.plist
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment