Set up GMail SMTP relay script

Hi, this script will install and configure a mail server on your Debian based Linux using GMail SMTP as a relay server.

Steps:

– Create a GMail account.
– Set up 2 factor authentication on GMail account
– Set up and write down an App Password for GMail account
– Download the script:

wget -c https://files.andrzejl.eu/gmail_relay.sh 

– Use GMail details to replace some data in the script below in your fav text editor
– Use GMail App Password to replace some data in the script below in your fav text editor
– Use the e-mail that’s suppose to receive the e-mails to replace some data in the script below in your fav text editor

mcedit ./gmail_relay.sh

OR

nano ./gmail_relay.sh

OR

vi ./gmail_relay.sh

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

chmod +x ./gmail_relay.sh

– Run the script as root twice

sudo ./gmail_relay.sh
sudo ./gmail_relay.sh

Here’s the code you’re downloading:


# Script written by AndrzejL and downloaded from https://andrzejl.eu/files/gmail_relay.sh
# Please do not remove or edit the first three lines. https://blog.andrzejl.eu/2022/10/25/set-up-gmail-smtp-relay-script/
# Please do not use this script for anything illegal. I cannot stop you but I hope you will respect my request.
# Replace all instances of e-mail_address_of_the_relay@gmail.com with actual gmail relay email address
# Make sure you have 2 factor authentication enabled on gmail
# Make sure you have a app password setup on gmail
# Replace all instances of YOURGMAILAPPPASSWORD with gmail app password
# Replace all instances of YourDifferent@Email.address with your recipient e-mail address
# Run this script twice and you're golden.
# IF asked - chose NO CONFIGURATION during the setup
# Check Your receipient e-mail after the script is ran
apt-get --reinstall -y install libsasl2-modules postfix bsd-mailx dialog &&
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 = 2' >> /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 e-mail_address_of_the_relay@gmail.com:YOURGMAILAPPPASSWORD' > /etc/postfix/sasl_passwd &&
echo '/.+/ e-mail_address_of_the_relay@gmail.com' > /etc/postfix/sender_canonical_maps &&
echo '/From:.*/ REPLACE From: e-mail_address_of_the_relay@gmail.com' > /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" YourDifferent@Email.address -a "FROM:e-mail_address_of_the_relay@gmail.com" &&
echo "Test message" | mail -s "test email" YourDifferent@Email.address -a "FROM:e-mail_address_of_the_relay@gmail.com" &&
echo MAILTO="YourDifferent@Email.address" &&
echo $HOSTNAME


Cheers.

Andrzej

AndrzejL

"Never meet Your heroes. Most of the time you'll only end up disappointed." White Polak Male Husband Employee Hetero Carnivorous Fugly Geek @$$hole with ADD Catholic “Some men just want to watch the world burn.”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.