Skip to content

Instantly share code, notes, and snippets.

@ChiefGyk ChiefGyk/mailgun_setup.sh
Last active Sep 28, 2018

Embed
What would you like to do?
sets up mail-in-a-box to work with mailgun for email relaying
#!/bin/bash
#Must make the sasl_passwd and relayhost_map files beforehand. Scroll down a bit for how they are to be formatted
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
#Remove the line that has "relayhost =" we will add it back later properly
sed -i.bak '/relayhost =/d' /etc/postfix/main.cf
#Look for and add the appropriate lines for mailgun
grep -q -F 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd' /etc/postfix/main.cf || echo 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd' >> /etc/postfix/main.cf
grep -q -F 'smtp_sasl_auth_enable = yes' /etc/postfix/main.cf || echo 'smtp_sasl_auth_enable = yes' >> /etc/postfix/main.cf
grep -q -F 'sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_map' /etc/postfix/main.cf || echo 'sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_map' >> /etc/postfix/main.cf
grep -q -F 'smtp_sender_dependent_authentication = yes' /etc/postfix/main.cf || echo 'smtp_sender_dependent_authentication = yes' >> /etc/postfix/main.cf
grep -q -F 'smtp_sasl_security_options = noanonymous' /etc/postfix/main.cf || echo 'smtp_sasl_security_options = noanonymous' >> /etc/postfix/main.cf
grep -q -F 'relayhost = [smtp.mailgun.org]:587' /etc/postfix/main.cf || echo 'relayhost = [smtp.mailgun.org]:587' >> /etc/postfix/main.cf
#Copy the sasl file into postfix
#NEEDS TO BE CREATED BEFORE RUNNING THIS USING THE FORMAT BELOW FOR ALL DOMAINS
#
#@domain.tld postmaster@domain.tld:mailgun-smtp-password
#@additional.tld postmaster@additional.tld:mailgun-smtp-password3
#[smtp.mailgun.org]:587 postmaster@domain.tld:mailgun-smtp-password
#[smtp.mailgun.org]:587 postmaster@additional.tld:mailgun-smtp-password3
#
cp sasl_passwd /etc/postfix/sasl_passwd
#Copy the relayhost_map file into postfix
#NEEDS TO BE CREATED BEFORE RUNNING THIS USING THE FORMAT BELOW FOR ALL DOMAINS
#
#@domain.tld [smtp.mailgun.org]:587
#@additional.tld [smtp.mailgun.org]:587
#
cp relayhost_map /etc/postfix/relayhost_map
#lockdown the files so they are only readable by root
chmod 600 /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/relayhost_map
#map the files in postfix
postmap /etc/postfix/sasl_passwd
postmap /etc/postfix/relayhost_map
#Restart postfix to have it work
service postfix restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.