Skip to content

Instantly share code, notes, and snippets.

@TimoDJatomika
Last active November 5, 2017 18:09
Show Gist options
  • Save TimoDJatomika/7bd6cc03b9f16b5ab343117f89e966dc to your computer and use it in GitHub Desktop.
Save TimoDJatomika/7bd6cc03b9f16b5ab343117f89e966dc to your computer and use it in GitHub Desktop.
autoconfig a relayhost in postfix
#!/bin/bash
# author: Timo Stankowitz <[email protected]>
# create date: 2016-08-15
# last change: 2017-11-
# Dieses Script konfiguriert Postfix so, dass mailgun (oder andere) als e-mail relay konfiguriert wird
# somit werden e-mails über mailgun, und nicht direkt von Postfix gesendet.
# Dies hat den Vorteil, dass die e-mails nicht im Spam Ordern, oder überhaupt nicht beim client landen.
postfixMainConfig="/etc/postfix/main.cf"
postfixSaslPasswdFile="/etc/postfix/sasl_passwd"
fqdn=$(hostname).example.de
relayhost="smtp.mailgun.org"
relayhostPort=587
smtpUserName="[email protected]"
smtpUserPassword=""
# first check if the user is root
if [ "$(id -u)" != 0 ]; then
echo "Please execute this script as root"
exit 1
fi
# check if postfix is installed on the system
if [ ! -f $postfixMainConfig ]; then
printf "Postfix is not installed. Do you wish to install postfix now? (Y/n): "
read
case $REPLY in
yes|Yes|y|Y|j|J|'')
echo "Installing Postfix now"
echo "Please select 'Internet Site' for the postfix configuration"
echo "and after that press enter for the hostname"
sleep 5
# check if apt-get is installed on the system
which apt-get > /dev/null 2>&1
checkAptGetExists=$?
if [ "$checkAptGetExists" != 0 ]; then
echo "ERROR: apt-get is not installed on your system."
printf "Please install apt-get on your system. \nEXIT"
exit 1
fi
apt-get update
apt-get install postfix -y
;;
n|N|no)
echo "Please install postfix manually."
exit 1
;;
esac
fi
echo
printf "Please Enter your FQDN [$fqdn]: "
read
if [ "$REPLY" != '' ]; then
fqdn=$REPLY
fi
echo "set FQDN to $fqdn"
echo
printf "Please set your relayhost [$relayhost]: "
read
if [ "$REPLY" != '' ]; then
relayhost=$REPLY
echo "set relayhost to $relayhost"
echo
printf "Please set to port of the relayhost [$relayhost] : "
read
if [ "$REPLY" != '' ]; then
relayhostPort=$REPLY
echo "set port to $relayhostPort"
fi
fi
echo "set relayhost to $relayhost"
echo
printf "Please set the smtp username [$smtpUserName]: "
read smtpUserName
echo "smtp username set to $smtpUserName"
echo
printf "Please set the smtp user password (will be displayed in plaintext): "
read smtpUserPassword
echo "smtp user password set to $smtpUserPassword"
# checking configuration
echo
echo "here is your configuration: "
echo
echo "FQDN: $fqdn"
echo "smtp relay host: $relayhost"
echo "smtp relay host port: $relayhostPort"
echo "smtp username: $smtpUserName"
echo "smtp user password: $smtpUserPassword"
echo
printf "is everything fine? (Y/n): "
read
case $REPLY in
yes|Yes|y|Y|j|J|'')
;;
n|N|no)
"oh shit... sorry... try again... EXIT"
exit 1
;;
esac
# create a backup of the existing postfix conf file
echo "creating a backup of the original config file"
cp $postfixMainConfig $postfixMainConfig.orig
# working on the postfix config file
# deleting those entries
sed -i '/appending .domain is the/d' $postfixMainConfig
sed -i '/append_dot_mydomain/d' $postfixMainConfig
sed -i '/relayhost/d' $postfixMainConfig
sed -i '/myhostname/d' $postfixMainConfig
sed -i '/mydestination/d' $postfixMainConfig
# append to the config file
echo >> $postfixMainConfig
echo "# custom stuff" >> $postfixMainConfig
echo "append_dot_mydomain = yes" >> $postfixMainConfig
echo >> $postfixMainConfig
echo "myhostname = $hostname" >> $postfixMainConfig
echo "mydestination = $hostname, localhost.localdomain, localhost" >> $postfixMainConfig
echo >> $postfixMainConfig
echo "relayhost = [$relayhost]:$relayhostPort" >> $postfixMainConfig
echo "smtp_sasl_auth_enable = yes" >> $postfixMainConfig
echo "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" >> $postfixMainConfig
echo "smtp_sasl_security_options = noanonymous" >> $postfixMainConfig
echo "smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt" >> $postfixMainConfig
echo "smtp_use_tls = yes" >> $postfixMainConfig
echo "[$relayhost]:$relayhostPort $smtpUserName:$smtpUserPassword" > $postfixSaslPasswdFile
postmap $postfixSaslPasswdFile
# change permission
chmod 600 /etc/postfix/sasl_passwd*
echo
echo "Configuration is now done. Restarting postfix now..."
sleep 2
/etc/init.d/postfix restart
# check if mailutils are installed
which mail > /dev/null 2>&1
checkMailInstalled=$?
if [ "$checkMailInstalled" != 0 ]; then
echo
printf "Mail tools are not installed on your system. Do you want to install the tools now? (Y/n): "
read
case $REPLY in
yes|Yes|y|Y|j|J|'')
apt-get install mailutils -y
;;
n|N|no)
echo "Okay - please install the tools manualy and try to send a test mail."
;;
esac
fi
echo
echo "DONE. Now you can test the configuration by sending a testmail."
echo "echo "this is just a test" | mail -s "[test] just a test" [email protected]"
echo
echo "If you receive any error check /var/log/syslog. "
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment