Mail Administration Guide

How sendmail Works

The sendmail program collects a message from a program like mailx or mailtool, edits the message header as required by the destination mailer, and calls appropriate mailers to deliver mail or to queue the mail for network transmission.


Note -

The sendmail program never edits or changes the body of a message. Any changes that it makes to interpret email addresses are made only in the header of the message.


Argument Processing and Address Parsing

When sendmail receives input, it collects recipient names (either from the command line or from the SMTP protocol) and generates two files. One is an envelope that contains the header and a list of recipients. The other file contains the body of the message. The sendmail program expands aliases, including mailing lists, and validates as much as possible of the remote recipient. Then sendmail checks syntax and verifies local recipients. Detailed checking of host names is deferred until delivery. As sendmail verifies local recipients, messages are forwarded to them.

After parsing the recipient lists, sendmail appends each name to both the envelope and the header of the message. When a name is aliased or forwarded, it retains the old name in the list and sets a flag to tell the delivery phase to ignore this recipient. The lists are kept free from duplicates, preventing alias loops and duplicate messages delivered to the same recipient, which can occur if a recipient is in two different alias groups.


Note -

Users may receive duplicate copies of the same message when alias lists contain email addresses for the same person using different syntax. The sendmail program cannot always match the email addresses as duplicates of one another.


Message Collection

Once all recipient names are parsed and verified, the message is collected. The message comes in two parts: a message header and a message body. The header and the body are separated by a blank line.

The header is formatted as a series of lines in this form:


field-name: field-value

For example, a sample header might be:


From: John Smith <Smith@colorado.edu>

field-value can be split across lines by starting the subsequent lines with a space or a tab. Some header fields have special internal meaning and have appropriate special processing. Other headers are simply passed through. Some header fields, like time stamps, may be added automatically.

No formatting requirements are imposed on the message body except that they must be lines of text. sendmail stores the header in memory and stores the body of the message in a temporary file. To simplify the program interface, the message is collected, even if no names are valid. If none of the names are valid, the message is returned to the sender with an error.


Note -

With DeskSet(TM) Mail Tool, users can transmit binary data. However, it must be encoded by Mail Tool. The sendmail program does not automatically encode binary data. Refer to the mailtool(1) man page for information about how to encode and decode binary data messages.


The message body is completely uninterpreted and untouched, except that lines beginning with a dot have the dot doubled when transmitted over an SMTP channel. The receiver strips this extra dot.

Message Delivery

The send queue is grouped by the receiving host before transmission to implement message batching. An argument list is built as the scan proceeds. Mail being sent to files is detected during the scan of the send list.

After a connection is established, sendmail makes the per-mailer changes to the header and sends the result to the mailer. If any mail is rejected by the mailer, a flag is set to invoke the return-to-sender function after all delivery is complete.

The sendmail program sends the message to the mailer using one of the same interfaces used to submit a message to sendmail (using the conventional UNIX argument vector or return status, communicating over a pair of UNIX pipes, or using SMTP over a TCP connection). Each copy of the message has a customized header.

Error Handling

When mail can't be delivered, the mailer catches and checks the status code, and a suitable error message is given as appropriate. The exit code must conform to a system standard. If a nonstandard exit code is used, sendmail transmits the message, "Services unavailable".

Queueing for Retransmission

If the mailer returns a "temporary failure" exit status, the message is queued. A control file describes the recipients and various other parameters. This control file is formatted as a series of lines, each describing a sender, a recipient, the time of submission, or some other parameter of the message. The header of the message is stored in the control file so that the associated data file in the queue is just the temporary file that was originally collected.

Return to Sender

If errors occur during processing, sendmail returns the message to the sender for retransmission. The letter can be mailed back or written to the dead.letter file in the sender's home directory.

Message Header Editing

Certain editing of the message header occurs automatically. Header lines can be inserted under control of the configuration file. Some lines can be merged; for example, a From: line and a Full-name: line can be merged under certain circumstances.

Configuration File

Almost all configuration information is read at runtime from a text file that includes macro definitions (the value of macros used internally), header declarations (the format of header lines that are specially processed and lines that are added or reformatted), mailer definitions (giving information like the location and characteristics of each mailer), and name-rewriting rules (a limited pattern-matching system used to rewrite names).