[Updated] I Wrote A GMail SMTP Relay Postfix Setup Script So That You Don’t Have To!

Hello beautiful peoples!

Setting up an e-mail relay can be a pain in the backside… so I wrote a script that will make this task a breeze. The design is very human… 😉🤣 (If you know – you know…) It will install and configure a postfix (it will overwrite your existing postfix config without asking any questions) on your Debian based Linux using GMail SMTP as a relay server.

Steps to follow:

– Create a GMail account that will be used as a relay.
– Set up 2 factor authentication on the relay GMail account
– Set up and write down (copy) an App Password for GMail account
– Download the script:

wget -c https://files.andrzej.langow.ski/gmail_relay.sh

Use Your favourite text editor to make changes to the script:

mcedit ./gmail_relay.sh

OR

nano ./gmail_relay.sh

OR

vi ./gmail_relay.sh

What is Your favourite Linux terminal text editor? Let me know in the comments!

Make sure you perform all the necessary edits (lines 8, 9 and 10):


IMPORTANT: GMail App Password will be generated in a format of ABCD EFGH IJKL MNOP. 16 characters, space after each 4. You need to remove those spaces. The GMAILAPPPASSWORD=ABCDEFGHIJKLMNOP is what it should look like. No spaces.

IMPORTANT: OTHEREMAILADDRESS is used only to test the relay. It won’t be used anywhere in the configuration itself.

– Save the script
– Make sure gmail_relay.sh is executable

chmod +x ./gmail_relay.sh

– Run the script in the terminal as root.

sudo ./gmail_relay.sh

Here’s the code you’re downloading:






# 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




I hope this helps!

Catch you on the flip side,

AndrzejL

Avatar photo

AndrzejL

“Some men just want to watch the world burn.”

Leave a Reply

Your email address will not be published. Required fields are marked *