Emailing program output automatically with mailx

Last revision July 20, 2004

From its earliest days, UNIX had a simple command-line driven email program, originally called mail, that could be used to send email messages from one account to another on the system. Berkeley UNIX improved this program to make it network aware and add features, and renamed it mailx. This program is available on pangea.

mailx can be used interactively to read and send email, and can also be called by other programs that want to automatically send output or messages via email. Much more capable email programs have replaced mailx for interactive use, but it is still valuable for programmatic use. For example, on pangea, system management scripts run every night to perform various checks and cleanups, and then send their results automatically via email to the system manager, using mailx.

Here is the basic information you need to call mailx from a program to send a file (or input from a pipeline) automatically via email.

In interactive use for sending email, mailx expects to be called with one or more arguments specifying the email addresses to which you want to send a message. It then expects to get lines of input on the standard input, which is the terminal in interactive use. The first line is taken to be the Subject: line of the message, and the rest of the input lines (up to an end-of-file mark, entered interactively as the CTRL-D key combination) are the body of the message.

To call mailx from a program, you must supply the email address of the recipient as argument, and then redirect standard input to a file or a pipeline of data coming from another program. But what about the subject line? Normally, you would not want mailx to take the first line of your file or pipeline output and make it into the Subject: line.

mailx will take an explicit -s option, followed by the subject text in quotes, to specify the Subject: line, rather than using the first line of its standard input.

So, for programmatic use, you would call mailx using the syntax shown below. Remember that the entire command must be typed on one line (or included in one system call from a program), even though your browser may break up the line while displaying it here.

  • Reading from a file created by your program:
          mailx -s "subject text in quotes" recipient@hostname < inputfile
    You have to substitute your actual data for the emphasized items in the syntax, such as subject text in quotes, recipient@hostname, and inputfile. A real life example from the system management scripts on pangea is:
          mailx -s "Daily run output" < /var/adm/daily
  • Reading input from a pipeline of output from another program:
          someprogram | mailx -s "subject text in quotes" recipient@hostname
    Again, you have to substitute your actual data for the emphasized items in the syntax. Here is a real-life example:
          /local/adm/checkdumptapes | mailx -s "Dump tape check results"

Comments or Questions?