One way to run sendmail
is to provide it with the name of a recipient as the only
command-line argument. For example, the following
sends a mail message to george
:
[1]
[1] For now, you can do this only with the system-supplied sendmail. It requires a configuration file that we have not yet written for our freshly compiled version.
%/usr/lib/sendmail george
Multiple recipients can also be given.
For example,
the following sends a mail message to george
, truman
,
and teddy
:
%/usr/lib/sendmail george,truman,teddy
The sendmail program accepts
two different kinds of command-line arguments.
Arguments that do not begin with a -
character (such as george
)
are assumed to be recipients.
Arguments that do begin with a -
character are taken as switches
that determine the behavior of sendmail.
In this chapter we will cover only a few of these switch-style command-line
arguments (see
Table 4.1).
The complete list of command-line switches, along with an
explanation of each, is presented in the reference part of this book
(Chapter 36, The Command Line).
Flag | Description |
---|---|
-b | Set operating mode |
-v | Run in verbose mode |
-d | Run in debugging mode |
The sendmail program can function in a number of different ways
depending on which form of -b
argument you use. One form, for example, causes
sendmail to display the contents of the queue.
Another causes sendmail
to rebuild the aliases database.
A complete list of the -b
command-line mode-setting switches is shown in
Table 4.2.
We will cover only a few in this chapter and will present the others
as this tutorial proceeds.
Form | Description |
---|---|
-ba | Use ARPAnet (Grey Book) protocols |
-bD | Run as a daemon, but don't fork |
-bd | Run as a daemon |
-bH | Purge persistent host status |
-bh | Print persistent host status |
-bi | Rebuild alias database |
-bm | Be a mail sender |
-bp | Print the queue |
-bs | Run SMTP on standard input |
-bt | Test mode: resolve addresses only |
-bv | Verify: don't collect or deliver |
-bz | Freeze the configuration file (obsolete) |
The effects of some of the options in Table 4.2 can also be achieved by running the sendmail executable using different names. Those names and a description of their results are shown in Table 4.3. Each name can be a hard link with, a symbolic link to, or a copy of sendmail.
Name | Form | Description |
---|---|---|
hoststat | -bh | Print persistent host status |
mailq | -bp | Display the queue |
newaliases | -bi | Initialize alias database |
purgestat | -bH | Purge persistent host status |
smtpd | -bd | Run as a daemon |
The sendmail program can run as a daemon in the background, listening for incoming mail from other machines. The sendmail program reads its configuration file only once, when it first starts as a daemon. It then continues to run forever, never reading the configuration file again. As a consequence, it will never see any changes to that configuration file.
When you change something in the sendmail.cf configuration file, you always need to kill and restart the sendmail daemon. But before you can kill the daemon, you need to know how to correctly restart it. This information is in the /etc/sendmail.pid file or one of your system rc files.
On a Berkeley UNIX-based system, for example, the daemon is usually started like this:
/usr/lib/sendmail -bd -q1h
The -bd
command-line switch specifies daemon mode.
The -q
switch tells sendmail
how often to look in its queue to process pending mail.
The -q1h
switch says to
process the queue at one (1
) hour (h
) intervals.
The actual command to start the sendmail daemon on your system may be different from what we've shown. If you manage many different brands of systems, you'll need to know how to start the daemon on all of them.
Killing and restarting the sendmail daemon became easier beginning with V8.7. A single command [2] will kill and restart the daemon:
[2] Provided that the daemon was originally started with a full pathname.
%kill -HUP `head -1 /etc/sendmail.pid`
This single command has the same effect as the two commands shown for V8.6 below.
Before you can start the sendmail daemon, you need to make sure there is not a daemon running already. Beginning with V8.6, the pid of the currently running daemon is found in the first line of the /etc/sendmail.pid file. The processes of killing the daemon looks like this:
%
kill `head -1 /etc/sendmail.pid`
After killing the currently running daemon, you can start a new daemon with the following simple command:
%
`tail -1 /etc/sendmail.pid`
Under old versions of sendmail you need to use the ps(1) program to find the pid of the daemon. How you run ps is different on BSD UNIX and System V UNIX. For BSD UNIX the command that you use and the output that it produces resemble the following:
%
ps ax | grep sendmail | grep -v grep
99 ? IW 0:07 /usr/lib/sendmail -bd -q1h
%
kill 99
Here the leftmost number printed by ps (the 99
) was used
to kill the daemon.
For System V-based systems you use different arguments for the ps command, and its output differs:
%
ps -ae | grep sendmail
99 ? 0:01 sendmail
%
kill 99
Under old versions of sendmail you must look in your system rc files for the way to restart sendmail. We showed how to find this information in Chapter 3, The Roles of sendmail (see Section 3.4, "Role as a Daemon").
If you forget to kill the daemon before starting a new one, you may see a stream of messages similar to the following, one printed every 5 seconds (probably to your console window):
getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use getrequests: cannot bind: Address already in use opendaemonsocket: server SMTP socket wedged: exiting
This shows that the attempt to run a second daemon failed. [3]
[3] Note that some multicast-capable versions of UNIX allow multiple sendmail daemons to run simultaneously. This is a known bug in the SO_REUSEADDR ioctl(2) call for TCP under multicasting. Contact your vendor for a fix.
The sendmail program can also
display the contents of its queue directory. It
can do this in two ways: by running as a program named mailq
or by being run as sendmail with the -bp
command-line switch.
Whichever way you run it, the contents
of the queue directory are printed.
If the queue is empty, sendmail prints the following:
Mail queue is empty
If, on the other hand, there is mail waiting in the queue, the output is far more verbose, possibly containing lines similar to these:
.ps 8 Mail Queue (1 requests) -Q-ID-- -Size- --Q-Time--- ------Sender/Recipient------ GAA29775* 702 Thu Mar 12 16:51 <[email protected]> Deferred: Host fbi.dc.gov is down <[email protected]>
Here, the output produced with the -bp
switch
shows that there is only one mail message in the queue. If there
were more, each entry would look pretty much the same as this. Each message
results in at least two lines of output.
The first
line shows details about the message and the sender. The
GAA29775
identifies this message in the queue.
The *
shows that this message is locked and currently being
processed.
The 702
is the size of the message body in bytes
(the size of the df
file as mentioned in
Section 3.1.2, "The Queue Directory").
The date shows when this message was originally queued.
The address shown is the name of the sender.
A second line may appear giving a reason for failure (if there was one). A message may be queued intentionally or because it couldn't be delivered.
The third and possibly subsequent lines show the addresses of the recipients.
The output produced by the -bp
switch is more fully covered in
Chapter 23, The Queue.
Because sendmail may have to search through thousands of names in the aliases file, a version of the file is stored in a separate dbm(3) or db(3) database format file. The use of a database significantly improves lookup speed.
Although sendmail can automatically update the database
whenever the aliases file is changed, there will be times
when you will want to rebuild it yourself.
You do this either by running sendmail using
the command newaliases
or with the -bi
command-line switch.
The following two commands do the same thing:
%
newaliases
%
/usr/lib/sendmail -bi
There will be a delay while sendmail rebuilds the aliases database; then a summary of what it did is printed:
/etc/aliases: 859 aliases, longest 615 bytes, 28096 bytes total
This line shows that the database
was successfully rebuilt. Beginning with V8.6 sendmail, multiple alias files
became possible, so each line (and there might be many) begins with the
name of an alias file. The information then displayed is the number
of aliases processed, the size of the biggest entry
to the right of the :
in the aliases file, and
the total number of bytes entered into the database.
Any mistakes in an alias file will also be printed here.
The aliases file and how to manipulate it are covered in Chapter 24, Aliases.
A handy tool for checking aliases
is the -bv
command-line switch. It causes
sendmail to recursively look up an alias
and report the ultimate real name
that it found.
To illustrate, consider the following aliases file:
animals: farmanimals,wildanimals
bill-eats: redmeat
birds: farmbirds,wildbirds
bob-eats: seafood,whitemeat
farmanimals: pig,cow
farmbirds: chicken,turkey
fish: cod,tuna
redmeat: animals
seafood: fish,shellfish
shellfish: crab,lobster
ted-eats: bob-eats,bill-eats
whitemeat: birds
wildanimals: deer,boar
wildbirds: quail
Although you can figure out what the name ted-eats
ultimately expands to, it is far easier to have sendmail
do it for you. By using sendmail, you have the
added advantage of being assured accuracy, which is especially
important in large and complex aliases files.
In addition to expanding aliases, the -bv
switch performs another important function. It verifies
whether or not the expanded aliases are in fact deliverable.
Consider the following one-line aliases file:
root: fred,larry
Assume that the user fred
is the system administrator
and has an account on the local machine. The user larry
,
however, has left, and his account has been removed. You can
run sendmail with the -bv
switch to find out whether both names are valid:
%/usr/lib/sendmail -bv root
This tells sendmail to verify the name root
from the aliases file. Since larry
(one of root
's
aliases) doesn't exist, the output produced looks like this:
larry... User unknown fred... deliverable: mailer local, user fred