Sending e-mail using shell scriptsMost script programmers already know how easy it is to send e-mail from within shell scripts. A simple invocation of the
The "mail" command accepts additional command line parameters, e.g.
Some common usage examples:
But how can we "attach" a file to a mail? We know that newer mail readers like Mozilla, Netscape Messenger or Microsoft Exchange have a way of displaying file attachments, e.g. images, ZIP files or audio files. How can we send some mail text (e.g. "See this picture of me surfing!") together with a binary file (e.g. "surfing.jpeg")?
The next chapter describes the traditional approach using "uuencode".
File attachments with "uuencode"Sending binary files does not work well for the Internet. We could send a binary file "surfing.jpeg" using this command:
but chances are high that the image will be unusable the time it arrives at the recipient.
During mail delivery the mail is relayed from one mail delivery agent to the next, until it finally arrives at the recipient. Each delivery agent may transform the mail message, e.g. by stripping the 8th bit of each character, removing NUL bytes (ASCII code 0), converting the end-of-line character LF ("line-feed", ASCII code 12) to a local representation (e.g. CR LF), or removing trailing space or TAB characters from each line.
Since only some characters are sure to arrive unmodified, the
traditional solution is to encode the mail from binary format to
a text format that is safe to transmit. The program used for
this is called "
The simplest way to send a file as a mail attachment is shown by the following example:
That's all! If Sylvia uses a current mail reader like Mozilla, Netscape Messenger or Microsoft Exchange, she will see a mail containing just one file attachment: the file "surfing.jpeg". In the command above we had to specify the file name two times: the first name denotes the input file to be encoded, and the second name is the file name the recipient will see.
This way we can include normal text, too:
The first "
This method of sending file attachments works fairly well, but still has some shortcomings:
The next method does not have this disadvantages. It works with most (newer) mail user agents and uses the "Multipurpose Internet Mail Extensions" (MIME).
File attachments with MIMEThe MIME Standard ("Multipurpose Internet Mail Extensions") defines an impressive list of new features for e-mail messages, e.g.
Other features are non-ASCII character sets for message bodies and message headers.
But how can shell programmers use these features effectively?
At first there has to be a command-line driven program to send MIME mails. There are some MIME packages available free of charge, the most common being
But check your local system before downloading one of these packages, many UNIX systems (e.g. Linux) come with one or more of them pre-installed.
mutt is an interactive, text-oriented e-mail client. Not very useful for usage in a script, one might think. But on closer view the program turns out to provide an easy way to send e-mails with file attachments. Have a look at the following example:
This command shows how to specify four important parts of an e-mail:
mutt always reads a text from standard input that
will become the main body of the e-mail, the text before the
attachment. If the mail should consist of attachments only, we
can either specify
or use an empty line as the mail body:
mutt does some work behind the scenes, e.g. it's looking at the file extensions of the attachments, and selects an appropriate MIME type for each. This works reasonably well (depending on the configuration of the system in general and mutt in particular), but if more control about MIME types and the sending of the e-mail is required, the next package may be more appropriate.
The remainder of this chapter will use programs of the MetaMail package for the examples. Don't worry if the command lines seem somewhat cryptic, at the end of this article we will present a script simplifying the sending of mail attachments.
MetaMail provides two programs to
This new knowledge enables us to send an audio file using
This invocation of "metasend" sends a mail to "email@example.com" (option -t) with the subject "Hear our son!" (option -s), without prompting for further arguments (option -b). Without the "-b" argument, the command would have asked for addresses to send "carbon copies" to. The file to attach is called "crying.au", and the file type is "audio/basic".
The file type (or "MIME-type") "audio/basic" gives the recipient's mail reader a hint on the contents of the file attachment. Some contents may be displayed directly (e.g. "text/plain", "image/gif"), for others the mail reader may call external programs to present the contents (e.g. "application/postscript", "audio/wav"). The relation between MIME content type and external program to call often is established using a file called "mailcap" (see mailcap(4), if available on your system). Mozilla or Netscape's Communicator allows to specify "helper applications" that are invoked depending on the MIME type.
An annoying property of "metasend" is the need to specify the
MIME type for each file. The script
simplifies this. The following example sends two files,
The script is easier to use because it determines the MIME type of the specified files by examining their file name extension, e.g. ".gif" or ".ps".
The command used to determine the MIME type is getmimetype.
Copyright © 1998-2022 Heiner Steven (firstname.lastname@example.org)