setup-postfix-gmail.sh

Paste Content

#!/bin/bash

echo "=== POSTFIX RELAY SETUP VIA GMAIL SMTP ==="

read -p "Enter your Gmail address (sender): " GMAIL
read -p "Enter your Gmail App Password (16 characters, no spaces): " APPPASS

# 1. Install dependencies
echo "→ Installing Postfix and dependencies..."
sudo apt update
sudo DEBIAN_FRONTEND=noninteractive apt install -y postfix mailutils libsasl2-modules

# 2. Create credentials file
echo "→ Creating Gmail SMTP credentials file..."
echo "[smtp.gmail.com]:587 $GMAIL:$APPPASS" | sudo tee /etc/postfix/sasl_passwd > /dev/null

# 3. Secure the credentials file
echo "→ Securing sasl_passwd file..."
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

# 4. Backup existing Postfix config
echo "→ Backing up original Postfix config to main.cf.bak..."
sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.bak

# 5. Update Postfix configuration
echo "→ Configuring Gmail SMTP relay in Postfix..."
sudo sed -i '/^relayhost =/d' /etc/postfix/main.cf
sudo tee -a /etc/postfix/main.cf > /dev/null <<EOF

relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
EOF

# 6. Restart Postfix
echo "→ Restarting Postfix..."
sudo systemctl restart postfix

# 7. Send test email
echo "→ Sending test email..."
echo "This is a test email sent from your VPS using Gmail SMTP." | mail -s "Gmail SMTP Relay Test" $GMAIL

echo "=== DONE! Please check your Gmail inbox ($GMAIL) or Spam folder. ✅"

Comments

No comments yet.