TOP

How does Postfix MTA receive mail?

Postfix MTA 如何接收郵件?

How does Postfix MTA receive mail from the outside world? please describe the routing from the outside email requesting to be sent until it is delivered to the user's mailbox.

When Postfix MTA (Mail Transfer Agent) receives an email from the outside world, the process of receiving and delivering that email to the user's mailbox involves multiple steps. Here’s a detailed description of how the email travels from an external sender to the user’s mailbox in a Postfix-based system:

Postfix MTA 如何接收來自外界的郵件?請描述從請求發送的外部電子郵件到發送到使用者郵箱的路由。

當 Postfix MTA(郵件傳輸代理)收到來自外界的電子郵件時,接收該電子郵件並將其傳送到使用者郵箱的過程涉及多個步驟。以下是在基於 Postfix 的系統中電子郵件如何從外部寄件者傳輸到使用者信箱的詳細描述:


1. DNS 查找和 MX 記錄

  • 當有人傳送電子郵件至某個網域(例如「user@example.com」)時,寄件者的MTA 首先查詢網域名稱系統(DNS) 以尋找收件者網域的MX(郵件交換)記錄(例如,“example.com”)。
  • MX 記錄告訴寄件者的 MTA 哪一台伺服器負責接收該網域的電子郵件。
  • 取得 MX 記錄後,寄件者的 MTA 嘗試與 MX 記錄指定的 Postfix 伺服器建立連線。

2. SMTP 連線建立

  • 寄件者的 MTA 透過連接埠 25(標準 SMTP 連接埠)或連接埠 587 開啟與 Postfix 的 SMTP(簡單郵件傳輸協定) 連接,以進行安全提交。
  • Postfix 在這些連接埠上偵聽傳入的郵件。

3. SMTP 握手與安全 (TLS/SSL)

  • 在 SMTP 握手期間,Postfix 和寄件者的 MTA 交換問候語並確定它們都支援的協定和加密(例如 TLS)。
  • 如果支援且需要加密,則使用 TLS 保護連線。

4. 寄件者和收件者驗證

  • 寄件 MTA 提供寄件者地址和收件者地址。
  • Postfix 在過程中執行多項檢查:
  • SPF(寄件者策略框架)DKIM(網域金鑰識別郵件)檢查:這些有助於驗證寄件者網域的合法性。
  • 收件者位址檢查:Postfix 驗證收件者的電子郵件地址是否存在於其係統中。
  • Postfix 使用其設定(例如「relay_domains」和「mydestination」參數)來決定是否應該接受給定收件者的電子郵件。

5. 將電子郵件排隊

  • 一旦電子郵件被接受,Postfix 會暫時將電子郵件儲存在其傳入佇列中(通常為 /var/spool/postfix/incoming)。
  • Postfix 使用不同的守護程式(例如「cleanup」和「qmgr」)來處理佇列中的電子郵件。

6. 郵件處理與內容過濾

  • Postfix 可能使用外部工具進行內容過濾(例如 SpamAssassin、Amavis)或防毒掃描。這些工具會掃描電子郵件中是否有垃圾郵件、惡意軟體或違反策略的行為。
  • 如果電子郵件通過過濾檢查,則會進入下一階段。

7. 本地交付或中繼

  • Postfix 檢查收件者的電子郵件是否用於本地傳送或需要轉發到另一個 MTA:
    • 本地投遞:如果收件人的網域與本地域匹配(在「mydestination」中配置),Postfix 將使用內部投遞代理(如「local」)或外部代理程式(如Dovecot)將電子郵件投遞到本地郵箱賽勒斯
    • 中繼到另一台伺服器:如果收件者不是本地的,Postfix 會根據收件者的網域將電子郵件轉送到適當的 MTA。

8. 發送至使用者信箱

  • 對於本地傳送,Postfix 要么將電子郵件傳送到郵箱文件,要么將其傳遞到本地郵件傳送代理 (MDA),例如 Dovecot(如果使用 IMAP/POP3)或其他 MDA。
    • 如果使用 Maildir 格式,電子郵件將傳送到使用者的特定資料夾(例如,/home/user/Maildir/)。
    • 如果使用 mbox 格式,電子郵件將附加到單一郵件檔案中。

9. 使用者存取電子郵件

  • 使用者現在可以使用 IMAPPOP3 協定透過電子郵件用戶端(例如 Thunderbird、Outlook)存取其電子郵件。
  • 這些協定由 IMAP/POP3 伺服器處理,例如與電子郵件儲存(Maildir 或 mbox)互動的 DovecotCourier

關鍵元件概述:

  • DNS:提供 MX 記錄以引導電子郵件流量。
  • Postfix:接收並處理電子郵件。
  • 內容過濾器:可選的垃圾郵件和病毒過濾器。
  • MDA(郵件傳送代理):將電子郵件傳送到使用者的郵箱(例如,Dovecot、Procmail)。
  • 郵箱:儲存使用者的電子郵件(Maildir 或 mbox 格式)。
  • 使用者存取:使用者透過 IMAP/POP3 用戶端存取電子郵件。

流程範例:

  1. 寄件者 發送電子郵件至 user@example.com
  2. 寄件者的 MTA 檢查「example.com」的 MX 記錄並找到 Postfix 的 IP。
  3. SMTP 連線 建立,Postfix 接收電子郵件。
  4. Postfix 處理電子郵件,執行安全檢查和過濾。
  5. Postfix 將電子郵件傳送至信箱中的 user@example.com
  6. 使用者透過電子郵件用戶端使用 IMAP/POP3 檢索電子郵件。

這概述了從外部發送電子郵件到在 Postfix 環境中傳遞到使用者郵箱的整個過程。

1. DNS Lookup and MX Records

  • When someone sends an email to a domain (e.g., user@example.com), the sender's MTA first queries the Domain Name System (DNS) to find the MX (Mail Exchange) records for the recipient’s domain (e.g., example.com).
  • The MX record tells the sender’s MTA which server is responsible for receiving emails for that domain.
  • After getting the MX record, the sender's MTA tries to establish a connection with the Postfix server specified by the MX record.

2. SMTP Connection Establishment

  • The sender’s MTA opens an SMTP (Simple Mail Transfer Protocol) connection to Postfix via port 25 (the standard SMTP port) or port 587 for secure submission.
  • Postfix listens on these ports for incoming mail.

3. SMTP Handshake and Security (TLS/SSL)

  • During the SMTP handshake, Postfix and the sender’s MTA exchange greetings and determine the protocols and encryption (such as TLS) they both support.
  • If encryption is supported and required, the connection is secured using TLS.

4. Sender and Recipient Validation

  • The sending MTA provides the sender's address and the recipient's address.
  • Postfix performs several checks during this process:
    • SPF (Sender Policy Framework) and DKIM (DomainKeys Identified Mail) checks: These help verify the legitimacy of the sender's domain.
    • Recipient Address Check: Postfix verifies if the recipient’s email address exists in its system.
  • Postfix uses its configuration (such as relay_domains and mydestination parameters) to determine if it should accept the email for the given recipient.

5. Queueing the Email

  • Once the email is accepted, Postfix temporarily stores the email in its incoming queue (usually /var/spool/postfix/incoming).
  • Postfix processes emails in queues using different daemons (such as cleanup and qmgr).

6. Mail Processing and Content Filtering

  • Postfix may use external tools for content filtering (e.g., SpamAssassin, Amavis) or antivirus scanning. These tools scan the email for spam, malware, or policy violations.
  • If the email passes the filtering checks, it moves to the next stage.

7. Local Delivery or Relay

  • Postfix checks if the recipient's email is for local delivery or needs to be relayed to another MTA:
    • Local Delivery: If the recipient’s domain matches the local domain (configured in mydestination), Postfix delivers the email to the local mailbox using an internal delivery agent like local or an external agent such as Dovecot or Cyrus.
    • Relay to Another Server: If the recipient is not local, Postfix forwards the email to the appropriate MTA based on the recipient’s domain.

8. Delivery to User's Mailbox

  • For local delivery, Postfix either delivers the email to a mailbox file or passes it to a local Mail Delivery Agent (MDA) such as Dovecot (if IMAP/POP3 is used) or another MDA.
    • If using Maildir format, the email is delivered into the user's specific folder (e.g., /home/user/Maildir/).
    • If using mbox format, the email is appended to the single mailbox file.

9. User Accesses the Email

  • The user can now access their email via an email client (e.g., Thunderbird, Outlook) using either IMAP or POP3 protocols.
  • These protocols are handled by an IMAP/POP3 server such as Dovecot or Courier that interacts with the email store (Maildir or mbox).

Overview of Key Components:

  • DNS: Provides MX records to direct email traffic.
  • Postfix: Receives and processes the email.
  • Content Filters: Optional spam and virus filters.
  • MDA (Mail Delivery Agent): Delivers the email to the user’s mailbox (e.g., Dovecot, Procmail).
  • Mailbox: Stores the user’s email (Maildir or mbox format).
  • User Access: The user accesses the email via IMAP/POP3 clients.

Example Flow:

  1. Sender sends an email to user@example.com.
  2. Sender’s MTA checks the MX record for example.com and finds Postfix’s IP.
  3. SMTP connection is established, and Postfix receives the email.
  4. Postfix processes the email, performing security checks and filtering.
  5. Postfix delivers the email to user@example.com in the mailbox.
  6. User retrieves the email using IMAP/POP3 via an email client.

This outlines the entire process from the email being sent from the outside to its delivery to the user's mailbox in a Postfix environment.