Skip to content

Instantly share code, notes, and snippets.

@zachbrowne
Created September 26, 2015 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachbrowne/ea0be9d3a5163887b24f to your computer and use it in GitHub Desktop.
Save zachbrowne/ea0be9d3a5163887b24f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
## Update
sudo apt-get update && sudo apt-get upgrade -y
## Update Hostname
echo "server" > /etc/hostname
hostname -F /etc/hostname
## Update hosts File
sudo mv /etc/hosts /etc/hosts.bak
touch /etc/hosts
cat > /etc/hosts <<EOF
127.0.0.1 localhost.localdomain localhost
45.33.1.26 server.default.systems server
45.33.1.26 mail.default.systems mail
2600:3c00::f03c:91ff:fec8:f546 server.default.systems server
2600:3c00::f03c:91ff:fec8:f546 mail.default.systems mail
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
## Setup Postfix with Personal Gmail Account
HOST=$"mail.default.systems"
function install_postfix() {
echo | debconf-set-selections <<EOF
postfix postfix/root_address string
postfix postfix/rfc1035_violation boolean false
postfix postfix/mydomain_warning boolean
postfix postfix/mynetworks string 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
postfix postfix/mailname string $HOST
postfix postfix/tlsmgr_upgrade_warning boolean
postfix postfix/recipient_delim string +
postfix postfix/main_mailer_type select Internet with smarthost
postfix postfix/destinations string $HOST, localhost.localdomain, localhost
postfix postfix/retry_upgrade_warning boolean
## Install postfix despite an unsupported kernel?
postfix postfix/kernel_version_warning boolean
postfix postfix/not_configured error
postfix postfix/sqlite_warning boolean
postfix postfix/mailbox_limit string 0
postfix postfix/relayhost string [smtp.gmail.com]:587
postfix postfix/procmail boolean false
postfix postfix/bad_recipient_delimiter error
postfix postfix/protocols select all
postfix postfix/chattr boolean false
EOF
echo "Postfix should be configured as Internet with smarthost"
apt-get install -q -y postfix mailutils libsasl2-2 ca-certificates libsasl2-modules zenity
}
if ! dpkg -s postfix >/dev/null;
then
install_postfix
else
echo "Postfix is already installed."
echo "You may consider removing it before running the script."
echo "You may do so with the following command:"
echo "apt-get remove postfix --purge"
echo
fi
if zenity --version >/dev/null; then
GMAIL_USER=$(zenity --entry --title="Gmail username" --text="Enter your gmail username:")
GMAIL_PASS=$(zenity --entry --title="Gmail password" --text="Enter your gmail password:")
else
read -p "Gmail Username: " GMAIL_USER
read -sp "Gmail Password: " GMAIL_PASS
fi
echo # an empty line
if [ -z "$GMAIL_USER" ]; then echo "No gmail username given. Exiting."; exit -1; fi
if [ -z "$GMAIL_PASS" ]; then echo "No gmail password given. Exiting."; exit -1; fi
if ! [ -f /etc/postfix/main.cf.original ]; then
cp /etc/postfix/main.cf /etc/postfix/main.cf.original
fi
tee /etc/postfix/main.cf >/dev/null <<EOF
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_loglevel = 1
smtp_tls_per_site = hash:/etc/postfix/tls_per_site
smtp_tls_CAfile = /etc/ssl/certs/Equifax_Secure_CA.pem
smtp_tls_session_cache_database = btree:/var/lib/postfix/smtp_tls_session_cache
EOF
echo "[smtp.gmail.com]:587 $GMAIL_USER:$GMAIL_PASS" | tee /etc/postfix/sasl_passwd >/dev/null
chmod 400 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
echo "smtp.gmail.com MUST" | tee /etc/postfix/tls_per_site >/dev/null
chmod 400 /etc/postfix/tls_per_site
postmap /etc/postfix/tls_per_site
service postfix restart
echo "Configuration done"
echo "Thank you for using this postfix configuration script." | mail -s "Email relaying configured at ${HOST}" $GMAIL_USER@gmail.com
echo "I have sent you a mail to $GMAIL_USER@gmail.com"
echo "This will confirm that the configuration is good."
echo "Please check your inbox at gmail."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment