Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # author: Timo Stankowitz <timo.stankowitz@gmail.com>
- # create date: 2016-08-15
- # last change: 2016-08-15
- # 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="username@example.com"
- 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 "smpt relay host port: $relayhostPort"
- echo "smtp username: $smtpUserName"
- echo "smpt 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 orig 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 wan't 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 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" foobar@example.com"
- echo
- echo "If you receve any error check /var/log/syslog. "
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement