Cara Setup Postfix dengan Gmail SMTP Relay di CentOS 7

Pengenalan

Postfix adalah Mail Transfer Agent (MTA) yang popular untuk Linux. Dalam artikel ini, saya akan tunjukkan cara configure Postfix untuk guna Gmail SMTP relay dengan authentication. Ini berguna untuk server yang perlu send email tapi tidak ada dedicated mail server.

Kenapa Guna Gmail SMTP Relay?

  1. Mudah setup – Tidak perlu configure mail server sendiri
  2. Reliable – Gmail infrastructure sangat stable
  3. Free – Guna Gmail account biasa
  4. Secure – Support TLS/SSL encryption
  5. No spam issues – Gmail reputation membantu deliverability

Prerequisites

Sebelum mula, pastikan anda ada:

  • CentOS 7 server (atau RHEL-based system)
  • Postfix installed
  • Gmail account dengan 2FA enabled
  • Root access ke server

Langkah 1: Install Dependencies

Pertama, install dependencies yang diperlukan:

yum install -y openssl-devel db4-devel cyrus-sasl-devel

Langkah 2: Install cyrus-sasl-plain dari Source

Masalah utama dengan CentOS 7 adalah package cyrus-sasl-plain tidak available dalam default repositories. Jadi kita perlu compile dari source:

cd /tmp
wget https://github.com/cyrusimap/cyrus-sasl/releases/download/cyrus-sasl-2.1.28/cyrus-sasl-2.1.28.tar.gz
tar -xzf cyrus-sasl-2.1.28.tar.gz
cd cyrus-sasl-2.1.28
./configure --enable-plain --enable-login --with-openssl=/usr --prefix=/usr/local
make -j$(nproc)
make install

Selepas compile, copy library ke system path:

cp /usr/local/lib/sasl2/libplain.so* /usr/lib64/sasl2/
ldconfig

Langkah 3: Dapatkan Gmail App Password

Gmail tidak allow direct password authentication untuk security. Kita perlu guna App Password:

  1. Enable 2FA pada Gmail account:
  2. Create App Password:

Langkah 4: Configure Postfix

4.1. Create SASL Password File

Create file untuk store Gmail credentials:

cat > /etc/postfix/sasl_passwd << 'EOF'
[smtp.gmail.com]:587    your-email@gmail.com:APP_PASSWORD
EOF

chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd

Penting: Ganti your-email@gmail.com dengan email anda dan APP_PASSWORD dengan App Password yang anda dapat.

4.2. Configure main.cf

Edit /etc/postfix/main.cf dan tambah configuration berikut:

# Gmail SMTP Relay
relayhost = [smtp.gmail.com]:587

# Gmail SMTP Authentication
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

4.3. Configure Sender Address Mapping (Optional)

Jika anda nak semua email dari server guna email Gmail anda sebagai sender address:

cat > /etc/postfix/generic << 'EOF'
root@yourhost.localdomain    your-email@gmail.com
root                          your-email@gmail.com
@yourhost.localdomain         your-email@gmail.com
EOF

postmap /etc/postfix/generic

Tambah dalam /etc/postfix/main.cf:

smtp_generic_maps = hash:/etc/postfix/generic

4.4. Configure Root Mail Forwarding (Optional)

Untuk forward semua root mail ke Gmail:

Edit /etc/aliases:

root:    your-email@gmail.com

Kemudian run:

newaliases
postalias /etc/aliases

Langkah 5: Restart Postfix

Selepas semua configuration, restart Postfix:

postfix check
postfix reload
# Atau restart jika perlu
systemctl restart postfix

Testing

Test Email

Hantar test email:

echo "Test email dari server" | sendmail your-email@gmail.com

Check Logs

Monitor log untuk tengok status:

tail -f /var/log/maillog

Success indicators:

  • status=sent (250 2.0.0 OK – Email berjaya dihantar!
  • relay=smtp.gmail.com[64.233.170.108]:587 – Connected ke Gmail

Error indicators:

  • Authentication Required – Check App Password
  • no mechanism available – Check libplain.so installation
  • STARTTLS command first – Check TLS configuration

Troubleshooting

Masalah: “Authentication Required”

Penyelesaian:

  1. Verify App Password betul dalam /etc/postfix/sasl_passwd
  2. Rebuild password map: postmap /etc/postfix/sasl_passwd && postfix reload
  3. Pastikan 2FA enabled pada Gmail account

Masalah: “no mechanism available”

Penyelesaian:

  1. Verify libplain.so exists: ls -la /usr/lib64/sasl2/libplain.so*
  2. Jika tidak ada, reinstall cyrus-sasl-plain
  3. Restart Postfix: systemctl restart postfix

Masalah: “STARTTLS command first”

Penyelesaian:

  1. Verify smtp_tls_security_level = may dalam main.cf
  2. Check TLS certificates: ls -la /etc/ssl/certs/ca-bundle.crt

Masalah: Connection timeout

Penyelesaian:

  1. Check firewall rules untuk port 587
  2. Verify DNS: dig smtp.gmail.com
  3. Check network connectivity

Security Best Practices

  1. Protect sasl_passwd file:chmod 600 /etc/postfix/sasl_passwd chown root:root /etc/postfix/sasl_passwd
  2. Guna App Passwords:
    • Jangan guna regular password
    • App Passwords lebih secure
    • Boleh revoke individually
  3. Regular backups:
    • Backup semua config files
    • Store dalam secure location

Kesimpulan

Dengan configuration ini, server anda boleh send email melalui Gmail SMTP relay dengan secure authentication. Ini sangat berguna untuk:

  • System notifications
  • Cron job reports
  • Application emails
  • Server alerts

Semua configuration files telah di-backup dalam /opt/configurations/postfix-gmail/ untuk reference masa depan.

References


Date: November 25, 2025
Author: System Administrator
Tags: Postfix, Gmail, SMTP, Email, CentOS 7, Linux

Leave a Comment