# Mail Transfer Agent (MTA) basics

> What an MTA does, the big three (Postfix, Sendmail, Exim), and how to set up aliases, forwarding and check the mail queue.

Source: https://aminbytes.dev/bytes/essential-system-services/mail-transfer-agents/


Even a server that never hosts anyone's inbox sends mail. Cron emails the output of jobs to their owner, `smartd` warns about a failing disk, `sudo` reports someone who isn't allowed to use it, and `logwatch` sends a daily summary. All of that goes through a **Mail Transfer Agent** on the machine. If it's missing or misconfigured, those warnings quietly disappear.

For LPIC-1 you don't configure a full mail server. You need to know which MTAs exist, how aliases and forwarding work, and the commands every MTA shares.

## Who does what in email

Email passes through three kinds of programs:

| Role | Job | Examples |
| --- | --- | --- |
| **MUA**, Mail User Agent | The program a person reads and writes mail with | `mail`, mutt, Thunderbird |
| **MTA**, Mail Transfer Agent | Accepts mail and passes it on to the next server using SMTP | Postfix, Sendmail, Exim |
| **MDA**, Mail Delivery Agent | Puts a message into the right mailbox | Postfix's `local`, procmail, Dovecot LDA |

MTAs talk to each other with **SMTP** (Simple Mail Transfer Protocol) on TCP port 25. Mail clients usually hand mail to their provider on port 587 (submission) or 465 (SMTP over TLS).

On a single Linux box, local mail for user `amin` ends up in a plain file: `/var/mail/amin` on Debian and Ubuntu, `/var/spool/mail/amin` on RHEL (where `/var/mail` is a symlink to it).

## The common MTAs

You don't have to configure these for the exam, just recognise them:

| MTA | Known for | Main config |
| --- | --- | --- |
| **Sendmail** | The original, from 1983. Powerful, but its config language is famously hard. Still in RHEL, marked deprecated. | `/etc/mail/sendmail.mc`, compiled with `m4` into `/etc/mail/sendmail.cf` |
| **Postfix** | Built as a safer, simpler Sendmail replacement, split into small processes with few privileges. The most common choice today and the standard MTA on RHEL and Fedora. | `/etc/postfix/main.cf`, `/etc/postfix/master.cf` |
| **Exim** | Flexible, single-process design. Debian's traditional default MTA (package `exim4`). | `/etc/exim4/` on Debian |

Only one MTA can own port 25 and `/usr/sbin/sendmail`, so distributions let you install just one. Debian's packages conflict with each other. RHEL uses the `alternatives` system:

```bash
alternatives --display mta          # RHEL/Fedora: which MTA provides sendmail, mailq...
```

 (Side note: Qmail was a fourth popular MTA in the late 1990s, written with security as its top goal. Its author offered a cash reward for the first security hole, and for years nobody could claim it.)

## The Sendmail emulation layer

Sendmail was everywhere for so long that a lot of software calls `/usr/sbin/sendmail` directly to send mail. PHP's `mail()` function and cron both do. So Postfix and Exim each ship a **Sendmail emulation layer**: their own `sendmail`, `mailq` and `newaliases` commands that accept the same options.

| Command | Same as | Does |
| --- | --- | --- |
| `sendmail` | | Accept a message on standard input and queue it |
| `mailq` | `sendmail -bp` | List the mail queue |
| `newaliases` | `sendmail -bi` | Rebuild the aliases database |
| `sendmail -q` | | Try to deliver everything in the queue now |

Each MTA also has native tools you may meet:

| Task | Postfix | Exim |
| --- | --- | --- |
| Show the queue | `postqueue -p` | `exim -bp` |
| Flush the queue | `postqueue -f` or `postfix flush` | `exim -qff` |
| Delete a message | `postsuper -d QUEUEID` | `exim -Mrm MSGID` |

## Try it: a local-only Postfix

You can test everything below in a Debian or Ubuntu container. Tell Postfix to deliver locally only, then start it:

```bash
docker run -it --rm --hostname web1 debian
echo "postfix postfix/main_mailer_type select Local only" | debconf-set-selections
apt update && DEBIAN_FRONTEND=noninteractive apt install -y postfix mailutils
postfix start
useradd -m amin
```

On a real Debian or Ubuntu machine, run `sudo apt install postfix mailutils` and pick **Local only** in the menu. On RHEL or Fedora: `sudo dnf install postfix s-nail` and `sudo systemctl enable --now postfix`.

## Sending and reading mail with mail

The `mail` command is a simple MUA. It comes from `mailutils` or `bsd-mailx` on Debian and Ubuntu, and from `s-nail` on RHEL and Fedora. To send:

```bash
echo "The backup finished at 02:00." | mail -s "Backup report" amin
mail -s "Disk report" amin < /tmp/df.txt
```

`-s` sets the subject. Without a pipe or redirect, `mail` reads the body from the keyboard until you press `Ctrl+D`.

Run `mail` with no arguments to read your own mailbox:

```bash
su - amin -c mail
```

```console
"/var/mail/amin": 2 messages 2 new
>N   1 root               Tue Dec  1 14:40  13/451   Backup report
 N   2 root               Tue Dec  1 14:41  13/449   Disk report
? 
```

At the `?` prompt, type a number to read that message, `d` to delete it, `r` to reply, `h` to list headers again and `q` to quit.

You can also talk to the Sendmail layer directly. With `-t`, it takes the recipients from the `To:` header:

```bash
printf 'To: amin\nSubject: Hello\n\nSent with sendmail -t\n' | sendmail -t
```

## Email aliases: /etc/aliases

An **alias** is an extra local name that delivers somewhere else. Aliases live in `/etc/aliases`, one per line, `name: target`:

```text {title="/etc/aliases"}
# required by the mail RFCs
postmaster:    root
# send root's mail to a real person
root:          amin
# one name, several people
support:       amin, sara
# an outside address
alerts:        oncall@example.com
# pipe the message into a program
tickets:       "|/usr/local/bin/new-ticket"
# append to a file
archive:       /var/mail-archive/archive.mbox
# read the recipient list from a file
team:          :include:/etc/mail/team.list
```

The MTA doesn't read this text file directly. It reads a compiled database built from it (such as `/etc/aliases.db`), which is much faster to search. So after **every** change, rebuild it:

```bash
sudo newaliases
```

Forgetting `newaliases` is the classic mistake: the file looks right, but nothing changes.

The `root: amin` line matters on real servers. Cron output, security warnings and failed jobs all go to root, and nobody reads root's mailbox. One alias sends them to someone who will.

## Forwarding a user's mail: ~/.forward

Aliases are system-wide and need root. A user can forward their **own** mail with a `.forward` file in their home directory. Each line is an address, a file, or a pipe:

```text {title="/home/amin/.forward"}
amin@example.com
```

That sends all of amin's mail to an outside address and keeps nothing locally. To keep a local copy too, add the local name with a backslash:

```text {title="/home/amin/.forward"}
\amin, amin@example.com
```

The backslash means "deliver to the local mailbox without looking at aliases or `.forward` again", so the forward doesn't loop.

A few rules:

- No `newaliases` needed: the MTA reads `~/.forward` every time it delivers.
- Keep the file owned by the user and not writable by the group or others. Sendmail, for one, ignores an unsafe `.forward` on purpose.
- Forwarding to outside addresses only works if the MTA is allowed to send mail to the internet. A "Local only" setup will queue those messages.

## Checking the mail queue with mailq

When a message can't be delivered right away (the remote server is down, DNS fails, the address is wrong), the MTA keeps it in a **queue** and retries later. `mailq` shows that queue:

```bash
mailq
```

```console
-Queue ID-  --Size-- ----Arrival Time---- -Sender/Recipient-------
4A7C21E0412     456 Tue Dec  1 14:52:03  root@web1
                       (connect to example.com[93.184.215.14]:25: Connection timed out)
                                         oncall@example.com

-- 0 Kbytes in 1 Request.
```

Each entry shows the queue ID, size, time, sender, the reason it's stuck, and the recipient. An empty queue says `Mail queue is empty`. Once you've fixed the cause, retry right away:

```bash
sudo sendmail -q          # works with any MTA
sudo postqueue -f         # the Postfix way
```

When something looks wrong, check the mail log: `/var/log/mail.log` on Debian and Ubuntu, `/var/log/maillog` on RHEL, or `journalctl -u postfix`.

## Exam checklist

- MUA = the reader (`mail`, mutt), MTA = the transporter (Postfix, Sendmail, Exim), MDA = the deliverer. SMTP uses port 25.
- Postfix: modern, common, default on RHEL. Sendmail: the oldest, `sendmail.cf`. Exim: Debian's traditional default.
- The Sendmail emulation layer gives every MTA the same `sendmail`, `mailq` (`sendmail -bp`) and `newaliases` (`sendmail -bi`) commands.
- `/etc/aliases` holds `name: target` lines. Targets can be users, addresses, `"|command"`, `/file` or `:include:/file`.
- Always run `newaliases` after editing `/etc/aliases`.
- `~/.forward` lists where a user's mail goes. `\user` keeps a local copy. No `newaliases` needed.
- `mail -s "subject" user` sends mail. `mail` alone reads `/var/mail/$USER`.
- `mailq` lists stuck messages. `sendmail -q` retries them.


## Byte check

1. You added `support: amin, sara` to `/etc/aliases`. What else must you do before it works?
   - **Run `newaliases`** (correct)
   - Run `mailq`
   - Nothing, the MTA reads the file on every message
   The MTA reads a compiled database, not the text file. `newaliases` (or `sendmail -bi`) rebuilds it.

2. User amin wants his mail sent to `amin@example.com` **and** kept in his local mailbox. What goes in `~/.forward`?
   - `amin@example.com`
   - **`\amin, amin@example.com`** (correct)
   - `amin: amin@example.com`
   The backslash in `\amin` delivers to the local mailbox without forwarding again. The `name: target` format belongs in `/etc/aliases`, not `~/.forward`.

3. Why does `mailq` work the same whether Postfix, Sendmail or Exim is installed?
   - It's part of the kernel
   - **Every MTA ships a Sendmail emulation layer with the same commands** (correct)
   - `mailq` only works with Sendmail
   Postfix and Exim both provide `/usr/sbin/sendmail`, `mailq` and `newaliases` that behave like Sendmail's, so scripts and tools keep working.


