# Script written by Andrzej Langowski and downloaded from https://files.andrzej.langow.ski/gmail_relay.sh
# Please do not remove or edit the first three lines. https://andrzej.langow.ski/2024/06/06/i-wrote-a-gmail-smtp-relay-postfix-setup-script-so-that-you-dont-have-to/
# Please do not use this script for anything illegal. I cannot stop you but I hope you will respect my request.
# Make sure you have 2 factor authentication enabled on gmail
# Make sure you have a app password setup on gmail
# Edit the three CHANGEME values below, change nothing else

GMAILRELAYADDRESS=CHANGEME@gmail.com
GMAILAPPPASSWORD=CHANGEME
OTHEREMAILADDRESS=CHANGEME@CHANGEME.CHANGEME

# Make this script executable and run it after editing the 3 lines above.

apt -y update &&
DEBIAN_FRONTEND=noninteractive apt --reinstall -yq install libsasl2-modules postfix bsd-mailx &&
cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf &&
date > /etc/postfix/main.cf  &&
echo "alias_maps = hash:/etc/aliases" > /etc/postfix/main.cf  &&
echo "alias_database = hash:/etc/aliases" >> /etc/postfix/main.cf  &&
echo "mynetworks = 127.0.0.0/8" >> /etc/postfix/main.cf  &&
echo "inet_interfaces = loopback-only" >> /etc/postfix/main.cf &&
echo "inet_protocols = ipv4" >> /etc/postfix/main.cf &&
echo "recipient_delimiter = +" >> /etc/postfix/main.cf &&
echo "compatibility_level = 3.6" >> /etc/postfix/main.cf &&
echo "relayhost = [smtp.gmail.com]:587" >> /etc/postfix/main.cf &&
echo "smtp_use_tls = yes" >> /etc/postfix/main.cf &&
echo "smtp_sasl_auth_enable = yes" >> /etc/postfix/main.cf &&
echo "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" >> /etc/postfix/main.cf &&
echo "smtp_tls_CApath = /etc/ssl/certs" >> /etc/postfix/main.cf &&
echo "smtp_sasl_security_options = noanonymous, noplaintext" >> /etc/postfix/main.cf &&
echo "smtp_sasl_tls_security_options = noanonymous" >> /etc/postfix/main.cf &&
echo "sender_canonical_classes = envelope_sender,header_sender" >> /etc/postfix/main.cf &&
echo "sender_canonical_maps = regexp:/etc/postfix/sender_canonical_maps" >> /etc/postfix/main.cf &&
echo "smtp_header_checks = regexp:/etc/postfix/header_check" >> /etc/postfix/main.cf &&
echo "[smtp.gmail.com]:587 $GMAILRELAYADDRESS:$GMAILAPPPASSWORD" > /etc/postfix/sasl_passwd &&
echo "/.+/    $GMAILRELAYADDRESS" > /etc/postfix/sender_canonical_maps &&
echo "/From:.*/ REPLACE From: $GMAILRELAYADDRESS" > /etc/postfix/header_check &&
/usr/sbin/postmap /etc/postfix/sasl_passwd &&
/usr/sbin/postmap /etc/postfix/sender_canonical_maps &&
/usr/sbin/postmap /etc/postfix/header_check &&
/usr/sbin/postalias /etc/aliases &&
postfix reload &&
systemctl enable postfix
systemctl start postfix &&
echo $HOSTNAME | mail -s "test email" $OTHEREMAILADDRESS -a "FROM:$GMAILRELAYADDRESS" &&
echo "Test message" | mail -s "test email" $OTHEREMAILADDRESS -a "FROM:$GMAILRELAYADDRESS" &&
echo MAILTO="$OTHEREMAILADDRESS" &&
# REPEATING EVERYTHING SECOND TIME
cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf &&
date > /etc/postfix/main.cf  &&
echo "alias_maps = hash:/etc/aliases" > /etc/postfix/main.cf  &&
echo "alias_database = hash:/etc/aliases" >> /etc/postfix/main.cf  &&
echo "mynetworks = 127.0.0.0/8" >> /etc/postfix/main.cf  &&
echo "inet_interfaces = loopback-only" >> /etc/postfix/main.cf &&
echo "inet_protocols = ipv4" >> /etc/postfix/main.cf &&
echo "recipient_delimiter = +" >> /etc/postfix/main.cf &&
echo "compatibility_level = 3.6" >> /etc/postfix/main.cf &&
echo "relayhost = [smtp.gmail.com]:587" >> /etc/postfix/main.cf &&
echo "smtp_use_tls = yes" >> /etc/postfix/main.cf &&
echo "smtp_sasl_auth_enable = yes" >> /etc/postfix/main.cf &&
echo "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" >> /etc/postfix/main.cf &&
echo "smtp_tls_CApath = /etc/ssl/certs" >> /etc/postfix/main.cf &&
echo "smtp_sasl_security_options = noanonymous, noplaintext" >> /etc/postfix/main.cf &&
echo "smtp_sasl_tls_security_options = noanonymous" >> /etc/postfix/main.cf &&
echo "sender_canonical_classes = envelope_sender,header_sender" >> /etc/postfix/main.cf &&
echo "sender_canonical_maps = regexp:/etc/postfix/sender_canonical_maps" >> /etc/postfix/main.cf &&
echo "smtp_header_checks = regexp:/etc/postfix/header_check" >> /etc/postfix/main.cf &&
echo "[smtp.gmail.com]:587 $GMAILRELAYADDRESS:$GMAILAPPPASSWORD" > /etc/postfix/sasl_passwd &&
echo "/.+/    $GMAILRELAYADDRESS" > /etc/postfix/sender_canonical_maps &&
echo "/From:.*/ REPLACE From: $GMAILRELAYADDRESS" > /etc/postfix/header_check &&
/usr/sbin/postmap /etc/postfix/sasl_passwd &&
/usr/sbin/postmap /etc/postfix/sender_canonical_maps &&
/usr/sbin/postmap /etc/postfix/header_check &&
/usr/sbin/postalias /etc/aliases &&
postfix reload &&
systemctl restart postfix &&
echo $HOSTNAME | mail -s "test email" $OTHEREMAILADDRESS -a "FROM:$GMAILRELAYADDRESS" &&
echo "Test message" | mail -s "test email" $OTHEREMAILADDRESS -a "FROM:$GMAILRELAYADDRESS" &&
echo MAILTO="$OTHEREMAILADDRESS" &&
echo $HOSTNAME
