Emails

The Email Process

There are three main parts of the Linux Email system:

  • Mail User Agent (MUA) is the program which the user uses to read and send e-mail. It reads incoming messages that have been delivered to the user's mailbox, and passes outgoing messages to an MTA for sending.
  • Mail Transfer Agent (MTA) acts as a "mail router". It accepts a message passed to it by either an MUA or another MTA, decides based upon the message header which delivery method it should use, and then passes the message to the appropriate MDA for that delivery method.
  • Mail Delivery Agent (MDA) accepts a piece of mail from an MTA and performs the actual delivery.

To illustrate how the three parts of the email system work together, here's a very general example. This is what happens when the user jsmith@host1.uiuc.edu sends e-mail to johndoe@host2.uiuc.edu:

  1. jsmith's MUA (pine, elm, etc) on host1.uiuc.edu passes the message to the MTA (sendmail) on the local host.
  2. The MTA (sendmail) notices that the message is addressed to a user at host2.uiuc.edu. Since it is configured to know that it can reach host2.uiuc.edu via SMTP, it passes the message to the SMTP MDA (the SMTP MDA is actually builtin to the MTA, but all other MDAs are external programs).
  3. The SMTP MDA connects to the MTA on host2.uiuc.edu (sendmail) and sends it the message.
  4. The MTA on host2.uiuc.edu (sendmail) notices that the message is addressed to a user on the local host, so it passes the message to the local MDA.
  5. The local MDA saves the message in user johndoe's mailbox.
  6. The next time johndoe logs in to host2.uiuc.edu and runs his MUA, the message is there waiting for him to read.

Overview

  • The user of an email client composes email and then sends it.
  • The email first goes to a local email server. The local email server then passes it to the local MDA, which is part of the local email server, which then send the email out via SMTP to a remote host.
  • The email server on the remote host recieves the mail via SMTP and then transfers it to the MDA which stores it (in the right place).
  • Users on the remote host can then retrieve the stored emails from the mailbox on the remote host via the POP3 or IMAP4 protocols.

MUA (Email Client)

An email client, email reader or, more formally, message user agent or mail user agent (MUA) is a computer program, or part of a computer program used to access and manage a user's email. It allows users to read, create, send, and receive email messages. An email client interfaces with a mail server (MTA) in order to route and transfer emails inside and between systems. Dedicated mail clients include Outlook or Thunderbird, or web clients like Yahoo Mail and GMail accessed through a browser.

MTA (Mail Server)

A Mail Transfer Agent (MTA) or email server or SMTP server is a program that receives and relays emails. An MTA receives messages from another MTA or from another email client. Postfix, Sendmail, Exim and Qmail, etc are all different implementations of email servers which use the Simple Mail Transfer Protocol (SMTP) for email transport over the Internet. The MTA is the most important piece of the email system. It is responsible for doing all the "intelligent" work of email transfer. While it does not actually perform any of the delivery itself, it is the part which tells the other parts how to interact and what to do. In a sense, the MTA is the glue which holds the whole process together. Many times multiple MTAs are used to talk with eachother such as in the case of a filtering via an SMTP relay, where one MTA filters emails and relays them to another MTA. MTAs do not handle any POP or IMAP communications, that is a job for MDAs.

MDA (Mail Delivery Agent)

A Mail Delivery Agent (MDA), also known as a Local Delivery Agent (LDA), is a computer program that deliveres incoming email to a recipient's mailbox. MDAs use the SMTP protocol to actually handle both the sending of outgoing email from the local host and also store incoming email passed to it from the MTA on the remote host. MDAs that handle outgoing mail are called an SMTP MDAs, and are ususally part of the MTA program itself, like Sendmail or Postfix. MDAs handling incoming mail are usually stand-alone programs like Procmail and Maildrop. The MDA recieves delivered email from the MTA and stores them in the recipient's local mailbox (file, directory, storage system). The IMAP and POP protocols are methods for retrieving email from a mailbox after delivery.

Mail Protocalls

There are three types of email protocols. SMTP transmits emails between servers, while POP and IMAP retrieve them from the mailbox on the server.

Neither POP nor IMAP send email messages. They are used by front-end programs (email clients) to simply retrieve emails from email server inboxes. In comparison, IMAP retains the message on the server. POP frees up storage space on servers, but IMAP frees up space on local hard drives. Both POP and IMAP have their advantages. However, POP works well for single devices, while IMAP is better when using multiple devices.

SMTP (Simple Mail Transfer Protocol)

SMTP is used to send and relay emails between email servers and clients. It ensures that emails are formatted properly and transmitted securely.

POP (Post Office Protocol)

POP (current iteration is POP3) is used to retrieve emails from a server. POP is a considered a legacy protocol and most providers no longer recommend it. POP downloads messages directly to your device and is a one-way sync from the email server inbox to the client, deleting the original message from the mail server, however it can be configured to keep an unread copy on the server. POP only downloads what’s in the inbox, not other folders like sent, spam, deleted items, etc.

IMAP (Internet Message Access Protocol)

IMAP (current iteration is IMAP4) is used to retrieve emails from a server. IMAP allows the sync of messages and message state between the clients and server. Since IMAP stores emails locally, you can access them anytime and anywhere. It supports many essential features for modern email usage, such as leaving messages on the server and accessing messages from multiple devices.

Mail Server Programs (MTAs)

Postfix and Sendmail serve a similar purpose. Both are Mail Transfer Agents (MTAs), which are responsible for routing and delivering email messages between mail servers.

Sendmail

Sendmail is a mail transfer agent (MTA) or email server. It was the first MTA on the Internet, created before the Internet officially existed. Like other MTAs, its purpose is to safely transfer email among hosts using the SMTP protocol. Properly configured Sendmail should be able to send email to any MTA on a reachable host. Nearly every modern *nix MTA is Sendmail compatible. While still in use in some environments, Sendmail has seen a decline in popularity in favor of Postfix and other alternative MTAs.

Postfix

Postfix is an MTA and like other MTAs, it accepts mail from the outside world and from local sources, and routes it to its destination. Postfix receives and delivers email messages over the network via the SMTP protocol. This may involve a connection to another machine, or it may involve delivering it to a local MDA or writing it directly to an mbox. For local delivery, the Postfix local delivery agent can deposit messages directly to a message store or hand off a message to an MDA.

Postfix emulates Sendmail's implementation. It implements the Sendmail CLI and is compatible with Sendmail mail filters. Other than that, it serves the same purpose, but accomplish it by different means. However for every mail server to succeed in the Unix environment, a sendmail binary (with some of the expected command line options) must be provided. Postfix has a focus on security, and it affords an impressive degree of flexibility and simple administration. That means it's easier for beginner admins to set up than other MTAs.

Installing Postfix

Install Postfix on Alpine Linux:

apk add postfix

The main configuration file for Postfix is main.cf. To setup Postfix for a pure localhost test, add these lines to the configuration file:

myhostname = localhost
mydomain = localdomain
inet_interfaces = $myhostname, localhost
mydestination = $myhostname, localhost.$mydomain, localhost
mynetworks_style = host

maillog_file = /var/log/postfix.log
smtpd_tls_loglevel = 1

Start the Postfix server:

# postfix start

The server is now ready to send emails. See Mailx below to run a test by sending an email to the root user. You can configure your websites to connect to localhost over TCP port 25 and they will not require any authentication.

Postfix can be configured to send mail out by relaying to another mail server you specify, with optional authentication. Getting email working with this type of system usually requires creating DNS records, and/or coordinating with existing mail servers, which will be the main constraint for this type of email setup.

Email Etc Aliases

The /etc/aliases file is part of sendmail, but Postfix and exim4 both use it as well. It helps manage email by defining aliases that determine where incoming mail should be delivered to. It can send mail to a local user account, to external email addresses, pipe the mail to programs, or simply direct mail into a file of your choice.

Every install is different, to find where your alias is located run the command postconf alias_maps. In the following example, the file should be located at /etc/postfix/aliases:

# postconf alias_maps
alias_maps = lmdb:/etc/postfix/aliases

Each line in the alias file has two parts. The mailbox name on the left side of the colon is expanded to the target(s) on the right. Here are a few entries to illustrate the file format:

root: localuser
ftp-bugs: joe,eric,paul
bit.bucket:  /dev/null
procmail: "|/usr/local/bin/procmail"
  • root: localuser Expands the root mailbox to the localuser mailbox, which is then looked up in the /etc/mail/aliases database. If no match is found, the message is delivered to localuser.
  • ftp-bugs: joe,eric,paul A mail list. Mail to ftp-bugs is expanded to the three local mailboxes joe, eric, and paul. A remote mailbox could be specified as user@example.com.
  • bit.bucket: /dev/null Shows how to write mail to a file, in this case /dev/null.
  • procmail: "|/usr/local/bin/procmail" Demonstrates how to send mail to a program, /usr/local/bin/procmail, through a UNIX pipe. Refer to aliases(5) for more information about the format of this file.

NOTE: Modifications to the /etc/aliases file are not complete until the newaliases command is run to build the /etc/aliases.db, which is the file actually used by the MTA. After running newaliases you must restart Postfix with postfix reload.

The root User

In the aliases file by default, most things are set to the root mailbox:

# Basic system aliases -- these MUST be present
MAILER-DAEMON:  postmaster
postmaster:     root
bin:            root
daemon:         root
named:          root
nobody:         root
uucp:           root
www:            root
ftp-bugs:       root
postfix:        root
manager:        root
dumper:         root
operator:       root
abuse:          postmaster

In Postfix, the root email is the username of the primary Linux account. By default, any system email that is generated by the system is sent to the root mailbox. So critical server errors, log errors, corn jobs alerts etc. all are sent to this default mailbox.

Most likely you will want a real email address to receive all the emails from the different system components. To do this you can set up an alias for the root user at the bottom of the aliases file:

# The address that should get root's mail.
root:           someone@somedomain.com

Mailx

Install mailx for programs that uses the mail command. Or for an easy command-line way to send email and check for and interact with the root mailbox.

Install Mailx on Alpine Linux:

# apk add mailx

To use Mailx to check the root users mailbox, simply type mail:

# mail
No mail for root

To send a test email, you also use the mail command, but with parameters.

echo "This is the email body" | mail -s "Email Test" root

In the following example we are sneding an email from the root user to the root user, and then checking the root mailbox. This is a good way to check that Postfix is setup correctly:

# echo "This is the email body" | mail -s "Email Test" root
# mail
Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/root": 1 message 1 new
>N  1 root@localhost.lo  Thu Aug 15 21:32   17/567   Email Test

Message 1:
From root@localhost.localdomain  Thu Aug 15 21:32:29 2024
X-Original-To: root
To: root@localhost.localdomain
Subject: Email Test
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit
Date: Thu, 15 Aug 2024 21:32:29 +0000 (UTC)
From: root <root@localhost.localdomain>

This is the email body