Skip to content

Instantly share code, notes, and snippets.

@JohnMorales
Last active December 18, 2018 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JohnMorales/d554e227146735217536462141fc6898 to your computer and use it in GitHub Desktop.
Save JohnMorales/d554e227146735217536462141fc6898 to your computer and use it in GitHub Desktop.
#!/bin/bash
#Configuring postfix to use aws ses as a gateway.
#https://gist.github.com/JohnMorales/d554e227146735217536462141fc6898
#update via `gist -u d554e227146735217536462141fc6898 config_mail.sh`
# Configuring postfix.
if [ -z "$AWS_ACCESS_KEY_ID" ]; then
echo "Must have AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY set"
exit 1
fi
domain=$1
machine=$2
if [ -z "$domain" ] || [ -z "$machine" ]; then
echo "Usage: $0 domain machine"
exit 1
fi
apt-get install -y postfix mailutils
if ! grep 'email-smtp.us-east-1.amazonaws' /etc/postfix/main.cf >/dev/null; then
cat <<POSTFIXCONF >> /etc/postfix/main.cf
relayhost = email-smtp.us-east-1.amazonaws.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_note_starttls_offer = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_generic_maps = hash:/etc/postfix/$domain
POSTFIXCONF
fi
cat <<POSTMAP > /etc/postfix/$domain
@$(hostname -f) $machine@$domain
@$(hostname) $machine@$domain
POSTMAP
postmap /etc/postfix/$domain
cat <<SASL_PASSWORD > /etc/postfix/sasl_passwd
email-smtp.us-east-1.amazonaws.com:587 $AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY
SASL_PASSWORD
postmap /etc/postfix/sasl_passwd
# Add any additional emails here comma separated, but keep mine so I can troubleshoot
if ! grep -E '^root: ' /etc/aliases >/dev/null; then
echo "root: john@$domain" >> /etc/aliases
newaliases
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment