متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 206Network · Postfix

How to send server mail through a relay with Postfix

Cron errors, backup reports, smartd warnings and app mails must reach a real mailbox — but mail sent straight from a server is often rejected as spam, and port 25 is blocked in most clouds. The clean way: a small send-only Postfix that listens only on localhost and hands every mail to a relay (your mail provider, Microsoft 365, SES, Mailgun) on port 587 with TLS and a login. This how-to sets it up, tests it, and reads the queue when mail does not arrive.

  • 5 الدروس
  • 5 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

206.1

The quick answer

postconf -e 'inet_interfaces = loopback-only'
postconf -e 'mydestination ='
postconf -e 'relayhost = [smtp.example.com]:587'
postconf -e 'smtp_sasl_auth_enable = yes'
postconf -e 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd'
postconf -e 'smtp_sasl_security_options = noanonymous'
postconf -e 'smtp_tls_security_level = encrypt'
install -m 600 /dev/null /etc/postfix/sasl_passwd
echo '[smtp.example.com]:587 alerts@example.com:APP-PASSWORD' > /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
systemctl restart postfix

206.2

Step by step

  1. Install Postfix ("Internet site" or "satellite" on Debian) and the SASL module: libsasl2-modules (Debian/Ubuntu) or cyrus-sasl-plain (RHEL).
  2. Send-only (quick answer, lines 1–2) — listen on localhost only, and treat no domain as local, so every mail goes to the relay.
  3. Relay with login (lines 3–6) — the brackets in [smtp.example.com] mean "no MX lookup". Use an app password or SMTP user from your provider.
  4. Force TLS (line 7) — encrypt refuses to send the password without TLS.
  5. Password file (lines 8–10) — readable by root only; postmap builds sasl_passwd.db. Run postmap again after every change.
  6. Sender address — many relays only accept mail from your verified address. Map local senders (root@web01) to it:
/etc/postfix/generic
root@web01.example.com    alerts@example.com
@web01.example.com        alerts@example.com
postconf -e 'smtp_generic_maps = hash:/etc/postfix/generic'
postmap /etc/postfix/generic && systemctl reload postfix
  1. Send a test:
echo "Test from $(hostname) at $(date)" | mail -s "Postfix relay test" ops@example.com
journalctl -u postfix -n 20

Look for status=sent (250 …).

  1. When mail does not arrive — read the queue and the reason:
mailq
postqueue -f

After fixing, postqueue -f tries again; postsuper -d ALL deferred deletes stuck mail.

206.3

Check that it worked

The journal shows relay=smtp.example.com[…]:587, … status=sent (250 …); the test mail is in the inbox (not in spam); mailq says Mail queue is empty; ss -ltn | grep :25 shows Postfix listening on 127.0.0.1 only.

الأوامر في هذا الدرس

206.4

Common problems

ProblemCauseFix
SASL authentication failedWrong user/password, or no app password with MFA.Check sasl_passwd; postmap again; use an app password.
no mechanism availableSASL module missing.Install libsasl2-modules / cyrus-sasl-plain.
Sender address rejectedRelay accepts only your verified sender.smtp_generic_maps to that address.
Connection timed out to port 587Firewall or provider blocks outgoing 587.Allow outgoing 587; check with nc -vz smtp.example.com 587.
Mail lands in spamSPF/DKIM of your domain do not cover the relay.Set SPF and DKIM for the relay provider.
mail: command not foundNo mail client installed.Install mailutils / s-nail, or use sendmail -t.

206.5

Go further

1Why use a relay instead of sending mail directly from the server?

Direct mail from servers is often blocked (port 25) or marked as spam; a relay with login, TLS, SPF and DKIM is trusted.

2What does smtp_tls_security_level = encrypt do?

Postfix only sends mail to the relay over TLS, so the login and the mail are never sent in plain text.

كل الأوامر في هذا الفصل