Ein SMTP-Relay leitet E-Mails über einen externen Mailserver weiter. Das ist nützlich wenn der eigene Server auf Blacklists steht oder bessere Zustellraten gewünscht sind.
Warum SMTP-Relay?
Vorteile
- Bessere E-Mail-Zustellbarkeit
- Umgehung von IP-Blacklists
- Professionelle E-Mail-Infrastruktur
- Statistiken und Tracking
- Authentifizierte AbsenderdomainsTypische Szenarien
1. Shared Hosting mit gesperrtem Port 25
2. Dynamische IP-Adressen
3. IP auf Blacklist
4. Hohe E-Mail-Volumina
5. Transaktions-E-Mails (Bestellungen, etc.)SMTP-Relay-Provider
Übersicht
| Provider | Free Tier | Preis | |----------|-----------|-------| | Mailgun | 100/Tag | Ab $0.80/1000 | | SendGrid | 100/Tag | Ab $14.95/Monat | | Amazon SES | 62.000/Monat* | $0.10/1000 | | Mailjet | 200/Tag | Ab $15/Monat | | Postmark | 100/Monat | Ab $15/Monat |
*Bei Nutzung von EC2
Postfix als Relay konfigurieren
Grundkonfiguration
# /etc/postfix/main.cf
# Relay Host
relayhost = [smtp.relay-provider.com]:587
# SASL-Authentifizierung
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
# TLS
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
# Header-Cleanup (optional)
smtp_header_checks = regexp:/etc/postfix/header_checksCredentials speichern
# /etc/postfix/sasl_passwd
[smtp.relay-provider.com]:587 username:password# Hashmap erstellen und Berechtigungen setzen
postmap /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.dbPostfix neu laden
systemctl restart postfixMailgun einrichten
Account erstellen
1. https://www.mailgun.com/
2. Account erstellen
3. Domain verifizieren
4. SMTP-Credentials erstellenDNS-Einträge
; Mailgun-Verifizierung
mg._domainkey.example.de. IN TXT "k=rsa; p=..."
example.de. IN TXT "v=spf1 include:mailgun.org ~all"Postfix-Konfiguration
# /etc/postfix/main.cf
relayhost = [smtp.mailgun.org]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt# /etc/postfix/sasl_passwd
[smtp.mailgun.org]:587 postmaster@mg.example.de:key-xxxxxxxxSendGrid einrichten
API-Key erstellen
1. https://sendgrid.com/
2. Settings → API Keys
3. Create API Key mit Mail Send PermissionPostfix-Konfiguration
# /etc/postfix/main.cf
relayhost = [smtp.sendgrid.net]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt# /etc/postfix/sasl_passwd
[smtp.sendgrid.net]:587 apikey:SG.xxxxxxxxxxxxxxAmazon SES einrichten
SMTP-Credentials
1. AWS Console → SES
2. SMTP Settings
3. Create SMTP Credentials
4. Access Key und Secret Key notierenPostfix-Konfiguration
# /etc/postfix/main.cf
relayhost = [email-smtp.eu-west-1.amazonaws.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_note_starttls_offer = yes# /etc/postfix/sasl_passwd
[email-smtp.eu-west-1.amazonaws.com]:587 AKIAIOSFODNN7EXAMPLE:wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYRegion anpassen
eu-west-1 → Irland
us-east-1 → N. Virginia
eu-central-1 → FrankfurtGmail als Relay
App-Passwort erstellen
1. Google-Konto → Sicherheit
2. Anmeldung bei Google → App-Passwörter
3. App-Passwort generierenPostfix-Konfiguration
# /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt# /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 user@gmail.com:app-passwortAchtung: Gmail hat Limits (500/Tag) und ist für Produktionsumgebungen nicht geeignet.
Sender-Rewriting
Problem
Absender: wordpress@server.example.de
Provider akzeptiert nur verifizierte Domains!Lösung: sender_canonical_maps
# /etc/postfix/main.cf
sender_canonical_maps = regexp:/etc/postfix/sender_canonical# /etc/postfix/sender_canonical
/.+/ noreply@example.depostmap /etc/postfix/sender_canonical
systemctl reload postfixHeader-Rewriting
# /etc/postfix/header_checks
/^From:.*/ REPLACE From: noreply@example.deConditional Relay
Nur bestimmte Domains über Relay
# /etc/postfix/main.cf
transport_maps = hash:/etc/postfix/transport# /etc/postfix/transport
example.de smtp:[smtp.mailgun.org]:587
*.example.de smtp:[smtp.mailgun.org]:587
* : # Alles andere direktpostmap /etc/postfix/transportBestimmte Domains ausschließen
# /etc/postfix/transport
internal.example.de local:
example.de smtp:[smtp.mailgun.org]:587Testen
Test-Mail senden
# Mit mail/mailx
echo "Test-Mail über Relay" | mail -s "Test" user@example.com
# Mit sendmail
echo "Subject: Test" | sendmail user@example.comLogs prüfen
tail -f /var/log/mail.log
# Erfolgreiche Zustellung
# status=sent (250 OK)
# Fehler
# status=bounced oder status=deferredSMTP-Test
# Mit swaks
apt install swaks
swaks --to user@example.com \
--from test@example.de \
--server localhost \
--port 25Troubleshooting
Authentifizierungsfehler
# Log prüfen
tail -f /var/log/mail.log | grep sasl
# Häufige Ursachen:
# - Falsches Passwort
# - sasl_passwd nicht gehasht (postmap vergessen)
# - Falsche BerechtigungenTLS-Probleme
# TLS-Test
openssl s_client -connect smtp.mailgun.org:587 -starttls smtp
# CA-Zertifikate prüfen
update-ca-certificatesRelay-Fehler
# "Relay access denied"
# → relayhost korrekt konfiguriert?
# → SASL aktiviert?
# "Connection refused"
# → Port blockiert?
# → Firewall?
telnet smtp.mailgun.org 587Queue prüfen
# Queue anzeigen
postqueue -p
# Mail-ID Details
postcat -q <queue-id>
# Queue leeren
postqueue -f
# Spezifische Mail löschen
postsuper -d <queue-id>Monitoring
Mail-Queue überwachen
#!/bin/bash
# /usr/local/bin/check-queue.sh
QUEUE=$(postqueue -p | tail -1 | cut -d' ' -f5)
if [ "$QUEUE" -gt 100 ]; then
echo "CRITICAL: Mail queue has $QUEUE messages"
exit 2
elif [ "$QUEUE" -gt 50 ]; then
echo "WARNING: Mail queue has $QUEUE messages"
exit 1
else
echo "OK: Mail queue has $QUEUE messages"
exit 0
fiLog-Auswertung
# Gesendete Mails zählen
grep "status=sent" /var/log/mail.log | wc -l
# Bounces zählen
grep "status=bounced" /var/log/mail.log | wc -l
# Top-Empfänger
grep "status=sent" /var/log/mail.log | \
grep -oP "to=<\K[^>]+" | \
sort | uniq -c | sort -rn | head -10Fallback-Konfiguration
Primärer und Backup-Relay
# /etc/postfix/main.cf
# Primärer Relay
relayhost = [smtp.primary.com]:587
# Fallback bei Fehler
smtp_fallback_relay = [smtp.backup.com]:587Retry-Einstellungen
# /etc/postfix/main.cf
# Schnellere Retries
queue_run_delay = 60s
minimal_backoff_time = 60s
maximal_backoff_time = 1h
# Bounce nach 1 Tag statt 5
bounce_queue_lifetime = 1d
maximal_queue_lifetime = 1dZusammenfassung
| Provider | Relay-Host | Port | |----------|------------|------| | Mailgun | smtp.mailgun.org | 587 | | SendGrid | smtp.sendgrid.net | 587 | | Amazon SES | email-smtp.region.amazonaws.com | 587 | | Gmail | smtp.gmail.com | 587 |
| Datei | Funktion | |-------|----------| | /etc/postfix/main.cf | Hauptkonfiguration | | /etc/postfix/sasl_passwd | Credentials | | /etc/postfix/transport | Routing | | /etc/postfix/sender_canonical | Absender-Rewriting |
| Befehl | Funktion | |--------|----------| | postmap file | Hashmap erstellen | | postqueue -p | Queue anzeigen | | postqueue -f | Queue flushen | | postfix reload | Konfiguration neu laden |
Fazit
SMTP-Relay ist eine einfache Lösung für bessere E-Mail-Zustellbarkeit. Die Konfiguration in Postfix erfordert nur wenige Zeilen. Professionelle Provider wie Mailgun oder SendGrid bieten zuverlässige Infrastruktur und Statistiken. Für transaktionale E-Mails (Bestellbestätigungen, Passwort-Reset) ist ein Relay-Provider oft die beste Wahl.