Lessons

Exim4 Mail Transfer Agent

Level: Advanced Module: Mail Services 9 min read Lesson 32 of 66

Overview

  • What you’ll learn: How to install and configure Exim4 as a Mail Transfer Agent on Ubuntu/Debian — including its split-configuration model, routing and transport architecture, TLS setup, and practical comparison with Postfix.
  • Prerequisites: Email Protocols (SMTP, IMAP, POP3), Postfix basics, DNS concepts
  • Estimated reading time: 18 minutes

Introduction

Exim4 is a powerful and highly configurable Mail Transfer Agent originally developed at the University of Cambridge. It is the default MTA on Debian-based systems (though Ubuntu defaults to Postfix). Exim4 takes a different approach to configuration than Postfix, using a single, highly flexible configuration language that gives administrators fine-grained control over mail routing, access control, and message processing.

While Postfix emphasizes simplicity and a “secure by default” architecture with separate programs for each function, Exim4 uses a monolithic design with a powerful built-in scripting language. This makes Exim4 extremely flexible but also means its configuration has a steeper learning curve.

In this lesson, you will learn how to install Exim4, understand its configuration model, set up basic mail delivery, configure TLS encryption, and understand when to choose Exim4 over Postfix.

Installing and Initial Configuration

On Debian-based systems, Exim4 can be installed from the default repositories. Ubuntu includes a convenient configuration tool called dpkg-reconfigure that walks you through the initial setup with a series of questions.

# Install Exim4
$ sudo apt update
$ sudo apt install exim4-daemon-heavy -y

# The "heavy" variant includes features like:
# - DKIM support
# - Database lookups (MySQL, PostgreSQL, SQLite)
# - Content scanning
# - LDAP support

# Run the interactive configuration wizard
$ sudo dpkg-reconfigure exim4-config

# Configuration choices:
# 1. General type: "internet site; mail is sent and received directly using SMTP"
# 2. System mail name: example.com
# 3. IP addresses to listen on: (leave empty for all interfaces)
# 4. Other destinations to accept mail for: example.com
# 5. Domains to relay for: (leave empty)
# 6. Machines to relay for: (leave empty)
# 7. Keep DNS queries minimal: No
# 8. Delivery method: Maildir format in home directory
# 9. Split configuration: Yes

# Verify installation
$ exim4 -bV
Exim version 4.96 #2 built 12-Jun-2023
...

# Check service status
$ sudo systemctl status exim4
● exim4.service - LSB: Mail Transport Agent
     Active: active (running)

# Verify Exim4 is listening on port 25
$ sudo ss -tlnp | grep :25
LISTEN  0  20  0.0.0.0:25  0.0.0.0:*  users:(("exim4",pid=3456,fd=3))

Split vs. Monolithic Configuration

Exim4 on Debian/Ubuntu supports two configuration modes:

  • Split configuration: Settings are spread across multiple files in /etc/exim4/conf.d/, organized by category (routers, transports, ACLs, etc.). This is the Debian default and recommended approach.
  • Monolithic configuration: Everything is in a single file /etc/exim4/exim4.conf.template. This is more similar to upstream Exim4 and may be preferred by administrators who want full control.
# Split configuration directory structure
$ ls /etc/exim4/conf.d/
acl/        auth/       main/       retry/      rewrite/
router/     transport/

# Key files in the split configuration:
# main/       — Global settings (hostname, domains, TLS)
# router/     — Mail routing rules (how to find the destination)
# transport/  — Delivery methods (how to deliver the message)
# acl/        — Access Control Lists (accept/reject decisions)
# auth/       — Authentication mechanisms
# retry/      — Retry rules for failed deliveries
# rewrite/    — Address rewriting rules

# After making changes, regenerate the configuration:
$ sudo update-exim4.conf

# Test the configuration
$ sudo exim4 -bV

# Reload Exim4
$ sudo systemctl reload exim4

Exim4 Routing and Transport Architecture

Exim4 processes mail through a pipeline of routers and transports. Routers determine where a message should go (local delivery, remote delivery, redirect, etc.), and transports determine how it gets there (file, pipe, SMTP, etc.). This separation provides great flexibility.

# View the default routers (in order of evaluation)
$ cat /etc/exim4/conf.d/router/200_exim4-config_primary

# Example: The default remote delivery router
dnslookup:
  driver = dnslookup
  domains = ! +local_domains
  transport = remote_smtp
  ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8
  no_more

# Example: The default local delivery router
localuser:
  driver = accept
  domains = +local_domains
  transport = maildir_home
  check_local_user

# View the default transports
$ cat /etc/exim4/conf.d/transport/30_exim4-config_remote_smtp

# Remote SMTP transport
remote_smtp:
  driver = smtp
  dkim_domain = ${lc:${domain:$h_from:}}
  dkim_selector = mail
  dkim_private_key = ${if exists{/etc/exim4/dkim/${dkim_domain}-private.pem}
                       {/etc/exim4/dkim/${dkim_domain}-private.pem}{0}}

# Local Maildir transport
maildir_home:
  driver = appendfile
  directory = $home/Maildir
  delivery_date_add
  envelope_to_add
  return_path_add
  maildir_format

TLS and Authentication

Exim4 supports TLS encryption for both incoming and outgoing connections. On Debian/Ubuntu, TLS can be configured through the split configuration files or the Debian-specific macros in /etc/exim4/conf.d/main/.

# Enable TLS in /etc/exim4/conf.d/main/03_exim4-config_tlsoptions

# TLS certificate and key
MAIN_TLS_ENABLE = yes
tls_certificate = /etc/letsencrypt/live/mail.example.com/fullchain.pem
tls_privatekey = /etc/letsencrypt/live/mail.example.com/privkey.pem

# Require TLS for authentication
tls_advertise_hosts = *
auth_advertise_hosts = ${if eq{$tls_in_cipher}{}{}{*}}

# Minimum TLS version
tls_require_ciphers = ECDHE-ECDSA-AES128-GCM-SHA256:
  ECDHE-RSA-AES128-GCM-SHA256:
  ECDHE-ECDSA-AES256-GCM-SHA384:
  ECDHE-RSA-AES256-GCM-SHA384

# Configure SMTP authentication
# Edit /etc/exim4/conf.d/auth/30_exim4-config_examples

plain_server:
  driver = plaintext
  public_name = PLAIN
  server_condition = "${if crypteq{$auth3}{${extract{1}{:}
    {${lookup{$auth2}lsearch{/etc/exim4/passwd}}}}}{1}{0}}"
  server_set_id = $auth2
  server_prompts = :
  server_advertise_condition = ${if eq{$tls_in_cipher}{}{}{*}}

# Regenerate configuration and restart
$ sudo update-exim4.conf
$ sudo systemctl restart exim4

# Test TLS connection
$ openssl s_client -starttls smtp -connect mail.example.com:25
250 HELP
EHLO test
250-mail.example.com Hello test
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-STARTTLS
250 HELP

Exim4 vs. Postfix: Comparison

Both Exim4 and Postfix are mature, production-ready MTAs, but they have different design philosophies and strengths:

# Quick comparison commands

# Check which MTA is installed
$ dpkg -l | grep -E 'exim4|postfix'

# Switch from Postfix to Exim4 (if needed)
$ sudo apt install exim4-daemon-heavy
$ sudo apt remove postfix

# Switch from Exim4 to Postfix
$ sudo apt install postfix
$ sudo apt remove exim4-daemon-heavy

# Exim4 mail queue management
$ sudo exim4 -bp          # View queue (like mailq)
$ sudo exim4 -qf          # Force delivery attempt (like postqueue -f)
$ sudo exim4 -Mrm MSG_ID  # Remove a message (like postsuper -d)

# Exim4 testing and debugging
$ sudo exim4 -bt user@example.com   # Test routing for an address
$ sudo exim4 -d -bt user@remote.com # Debug routing with verbose output
$ sudo exim4 -bP                     # Show all configuration settings
  • Configuration: Postfix uses simple key=value pairs in main.cf; Exim4 uses a full scripting language with conditions, lookups, and string expansions.
  • Architecture: Postfix is modular (separate processes); Exim4 is monolithic (single binary handles everything).
  • Flexibility: Exim4 is more flexible for complex routing and filtering scenarios; Postfix is simpler for standard configurations.
  • Default: Exim4 is the Debian default; Postfix is the Ubuntu default and more common in the broader Linux ecosystem.

Key Takeaways

  • Exim4 is a powerful MTA with a flexible scripting configuration language; it is the default on Debian while Postfix is the default on Ubuntu.
  • Debian/Ubuntu uses a split configuration model under /etc/exim4/conf.d/ with categories for routers, transports, ACLs, and authentication; run update-exim4.conf after changes.
  • Mail processing flows through routers (where to deliver) and transports (how to deliver), providing fine-grained control over mail routing.
  • TLS and authentication should always be enabled; Exim4 can use Let’s Encrypt certificates and supports PLAIN/LOGIN authentication with TLS-only advertising.
  • Choose Exim4 when you need complex routing logic, deep customization, or are running on Debian; choose Postfix for simpler setups, better documentation, and wider community support.

What’s Next

In the next lesson, you will learn how to integrate all the components — Postfix, Dovecot, OpenDKIM, and DNS authentication — into a complete, production-ready mail stack.

繁體中文

概述

  • 學習目標:學習如何在 Ubuntu/Debian 上安裝和設定 Exim4 作為郵件傳輸代理,包括其分割設定模型、路由和傳輸架構、TLS 設定,以及與 Postfix 的實際比較。
  • 先決條件:電子郵件協定(SMTP、IMAP、POP3)、Postfix 基礎、DNS 概念
  • 預計閱讀時間:18 分鐘

簡介

Exim4 是一個功能強大且高度可設定的郵件傳輸代理,最初在劍橋大學開發。它是 Debian 系統上的預設 MTA(儘管 Ubuntu 預設使用 Postfix)。Exim4 採用與 Postfix 不同的設定方式,使用單一、高度靈活的設定語言,讓管理員對郵件路由、存取控制和訊息處理有精細的控制。

Postfix 強調簡潔和「預設安全」的架構,每個功能使用獨立程式,而 Exim4 使用單體設計,內建強大的腳本語言。這使得 Exim4 極其靈活,但也意味著其設定有更陡峭的學習曲線。

在本課程中,您將學習如何安裝 Exim4、了解其設定模型、設定基本郵件投遞、設定 TLS 加密,以及了解何時選擇 Exim4 而非 Postfix。

安裝和初始設定

在 Debian 系統上,Exim4 可從預設儲存庫安裝。Ubuntu 包含一個方便的設定工具 dpkg-reconfigure,它會通過一系列問題引導您完成初始設定。

# 安裝 Exim4
$ sudo apt update
$ sudo apt install exim4-daemon-heavy -y

# 執行互動式設定精靈
$ sudo dpkg-reconfigure exim4-config

# 驗證安裝
$ exim4 -bV

# 檢查服務狀態
$ sudo systemctl status exim4

分割與單體設定

Debian/Ubuntu 上的 Exim4 支援兩種設定模式:

  • 分割設定:設定分散在 /etc/exim4/conf.d/ 中的多個檔案中,按類別組織(路由器、傳輸、ACL 等)。這是 Debian 的預設且建議的方式。
  • 單體設定:所有內容都在單一檔案 /etc/exim4/exim4.conf.template 中。
# 分割設定目錄結構
$ ls /etc/exim4/conf.d/
acl/  auth/  main/  retry/  rewrite/  router/  transport/

# 變更後重新產生設定:
$ sudo update-exim4.conf

# 測試設定
$ sudo exim4 -bV

# 重新載入 Exim4
$ sudo systemctl reload exim4

Exim4 路由和傳輸架構

Exim4 透過路由器傳輸的管線處理郵件。路由器決定郵件應該去哪裡(本地投遞、遠端投遞、重導向等),傳輸決定如何到達那裡(檔案、管道、SMTP 等)。

# 遠端投遞路由器範例
dnslookup:
  driver = dnslookup
  domains = ! +local_domains
  transport = remote_smtp
  no_more

# 本地投遞路由器範例
localuser:
  driver = accept
  domains = +local_domains
  transport = maildir_home
  check_local_user

# Maildir 傳輸
maildir_home:
  driver = appendfile
  directory = $home/Maildir
  maildir_format

TLS 和身份驗證

Exim4 支援傳入和傳出連線的 TLS 加密。

# 在 /etc/exim4/conf.d/main/ 中啟用 TLS
MAIN_TLS_ENABLE = yes
tls_certificate = /etc/letsencrypt/live/mail.example.com/fullchain.pem
tls_privatekey = /etc/letsencrypt/live/mail.example.com/privkey.pem
tls_advertise_hosts = *

# 重新產生設定並重新啟動
$ sudo update-exim4.conf
$ sudo systemctl restart exim4

Exim4 與 Postfix 比較

  • 設定:Postfix 在 main.cf 中使用簡單的 key=value 對;Exim4 使用完整的腳本語言。
  • 架構:Postfix 是模組化的(獨立行程);Exim4 是單體的(單一二進位檔處理所有事情)。
  • 靈活性:Exim4 在複雜路由和過濾場景中更靈活;Postfix 在標準設定中更簡單。
  • 預設值:Exim4 是 Debian 預設值;Postfix 是 Ubuntu 預設值且在更廣泛的 Linux 生態系統中更常見。

重點摘要

  • Exim4 是一個功能強大的 MTA,具有靈活的腳本設定語言;它是 Debian 的預設值,而 Postfix 是 Ubuntu 的預設值。
  • Debian/Ubuntu 在 /etc/exim4/conf.d/ 下使用分割設定模型;變更後執行 update-exim4.conf
  • 郵件處理通過路由器(投遞到哪裡)和傳輸(如何投遞),提供對郵件路由的精細控制。
  • 應始終啟用 TLS 和身份驗證;Exim4 可以使用 Let’s Encrypt 憑證並支援僅 TLS 通告的 PLAIN/LOGIN 身份驗證。
  • 當需要複雜的路由邏輯、深度自訂或在 Debian 上運行時選擇 Exim4;對於更簡單的設定、更好的文件和更廣泛的社群支援選擇 Postfix。

下一步

在下一課中,您將學習如何將所有元件——Postfix、Dovecot、OpenDKIM 和 DNS 驗證——整合到一個完整的、適用於生產環境的郵件堆疊中。

日本語

概要

  • 学習内容:Ubuntu/Debian での Exim4 のインストールと設定方法を学びます。分割設定モデル、ルーティングとトランスポートアーキテクチャ、TLS 設定、Postfix との実用的な比較を含みます。
  • 前提条件:メールプロトコル(SMTP、IMAP、POP3)、Postfix の基礎、DNS の概念
  • 推定読了時間:18分

はじめに

Exim4 は、元々ケンブリッジ大学で開発された強力で高度に設定可能なメール転送エージェントです。Debian ベースのシステムのデフォルト MTA です(ただし Ubuntu は Postfix がデフォルト)。Exim4 は Postfix とは異なる設定アプローチを取り、管理者がメールルーティング、アクセス制御、メッセージ処理をきめ細かく制御できる、単一の高度に柔軟な設定言語を使用します。

Postfix はシンプルさと各機能に個別のプログラムを使用する「デフォルトで安全」なアーキテクチャを重視する一方、Exim4 は強力な組み込みスクリプト言語を持つモノリシック設計を使用します。これにより Exim4 は非常に柔軟ですが、設定の学習曲線が急であることも意味します。

このレッスンでは、Exim4 のインストール、設定モデルの理解、基本的なメール配信の設定、TLS 暗号化の設定、Exim4 と Postfix のどちらを選ぶべきかの判断方法を学びます。

インストールと初期設定

Debian ベースのシステムでは、Exim4 はデフォルトリポジトリからインストールできます。Ubuntu には dpkg-reconfigure という便利な設定ツールがあり、一連の質問で初期設定を案内します。

# Exim4 のインストール
$ sudo apt update
$ sudo apt install exim4-daemon-heavy -y

# インタラクティブ設定ウィザードの実行
$ sudo dpkg-reconfigure exim4-config

# インストールの確認
$ exim4 -bV

# サービス状態の確認
$ sudo systemctl status exim4

分割設定 vs モノリシック設定

Debian/Ubuntu の Exim4 は2つの設定モードをサポートしています:

  • 分割設定:設定が /etc/exim4/conf.d/ 内の複数ファイルに分散され、カテゴリ別に整理されます(ルーター、トランスポート、ACL など)。Debian のデフォルトで推奨されるアプローチです。
  • モノリシック設定:すべてが単一ファイル /etc/exim4/exim4.conf.template に含まれます。
# 分割設定ディレクトリ構造
$ ls /etc/exim4/conf.d/
acl/  auth/  main/  retry/  rewrite/  router/  transport/

# 変更後に設定を再生成:
$ sudo update-exim4.conf

# Exim4 のリロード
$ sudo systemctl reload exim4

Exim4 のルーティングとトランスポートアーキテクチャ

Exim4 はルータートランスポートのパイプラインを通じてメールを処理します。ルーターはメッセージの送り先(ローカル配信、リモート配信、リダイレクトなど)を決定し、トランスポートはそこへの到達方法(ファイル、パイプ、SMTP など)を決定します。

# リモート配信ルーターの例
dnslookup:
  driver = dnslookup
  domains = ! +local_domains
  transport = remote_smtp
  no_more

# ローカル配信ルーターの例
localuser:
  driver = accept
  domains = +local_domains
  transport = maildir_home
  check_local_user

# Maildir トランスポート
maildir_home:
  driver = appendfile
  directory = $home/Maildir
  maildir_format

TLS と認証

Exim4 は受信・送信両方の接続で TLS 暗号化をサポートしています。

# /etc/exim4/conf.d/main/ で TLS を有効化
MAIN_TLS_ENABLE = yes
tls_certificate = /etc/letsencrypt/live/mail.example.com/fullchain.pem
tls_privatekey = /etc/letsencrypt/live/mail.example.com/privkey.pem
tls_advertise_hosts = *

# 設定を再生成して再起動
$ sudo update-exim4.conf
$ sudo systemctl restart exim4

Exim4 vs Postfix の比較

  • 設定:Postfix は main.cf でシンプルな key=value ペアを使用。Exim4 は条件、ルックアップ、文字列展開を持つ完全なスクリプト言語を使用。
  • アーキテクチャ:Postfix はモジュラー(個別プロセス)。Exim4 はモノリシック(単一バイナリがすべてを処理)。
  • 柔軟性:Exim4 は複雑なルーティングとフィルタリングシナリオに対してより柔軟。Postfix は標準的な設定に対してよりシンプル。
  • デフォルト:Exim4 は Debian のデフォルト。Postfix は Ubuntu のデフォルトで、より広い Linux エコシステムで一般的。

重要ポイント

  • Exim4 は柔軟なスクリプト設定言語を持つ強力な MTA です。Debian のデフォルトであり、Postfix は Ubuntu のデフォルトです。
  • Debian/Ubuntu は /etc/exim4/conf.d/ 下の分割設定モデルを使用します。変更後は update-exim4.conf を実行してください。
  • メール処理はルーター(配信先)とトランスポート(配信方法)を通じて流れ、メールルーティングのきめ細かな制御を提供します。
  • TLS と認証は常に有効にする必要があります。Exim4 は Let’s Encrypt 証明書を使用でき、TLS のみでの PLAIN/LOGIN 認証をサポートします。
  • 複雑なルーティングロジック、深いカスタマイズ、または Debian での実行が必要な場合は Exim4 を選択。よりシンプルなセットアップ、より良いドキュメント、より広いコミュニティサポートが必要な場合は Postfix を選択。

次のステップ

次のレッスンでは、すべてのコンポーネント——Postfix、Dovecot、OpenDKIM、DNS 認証——を完全な本番対応メールスタックに統合する方法を学びます。

You Missed