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?
- Mudah setup – Tidak perlu configure mail server sendiri
- Reliable – Gmail infrastructure sangat stable
- Free – Guna Gmail account biasa
- Secure – Support TLS/SSL encryption
- 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:
- Enable 2FA pada Gmail account:
- Pergi ke: https://myaccount.google.com/security
- Enable “2-Step Verification”
- Create App Password:
- Pergi ke: https://myaccount.google.com/apppasswords
- Select “Mail” dan “Other (Custom name)”
- Name: “Postfix Server”
- Copy 16-character password yang diberikan
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 Passwordno mechanism available– Check libplain.so installationSTARTTLS command first– Check TLS configuration
Troubleshooting
Masalah: “Authentication Required”
Penyelesaian:
- Verify App Password betul dalam
/etc/postfix/sasl_passwd - Rebuild password map:
postmap /etc/postfix/sasl_passwd && postfix reload - Pastikan 2FA enabled pada Gmail account
Masalah: “no mechanism available”
Penyelesaian:
- Verify libplain.so exists:
ls -la /usr/lib64/sasl2/libplain.so* - Jika tidak ada, reinstall cyrus-sasl-plain
- Restart Postfix:
systemctl restart postfix
Masalah: “STARTTLS command first”
Penyelesaian:
- Verify
smtp_tls_security_level = maydalam main.cf - Check TLS certificates:
ls -la /etc/ssl/certs/ca-bundle.crt
Masalah: Connection timeout
Penyelesaian:
- Check firewall rules untuk port 587
- Verify DNS:
dig smtp.gmail.com - Check network connectivity
Security Best Practices
- Protect sasl_passwd file:
chmod 600 /etc/postfix/sasl_passwd chown root:root /etc/postfix/sasl_passwd - Guna App Passwords:
- Jangan guna regular password
- App Passwords lebih secure
- Boleh revoke individually
- 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
- Postfix Documentation: http://www.postfix.org/documentation.html
- Gmail SMTP Settings: https://support.google.com/mail/answer/7126229
- Cyrus SASL: https://www.cyrusimap.org/sasl/
Date: November 25, 2025
Author: System Administrator
Tags: Postfix, Gmail, SMTP, Email, CentOS 7, Linux