On a Linux/Unix system or OS X system, a very quick way to send email messages and files by email from a shell prompt, aka command line, is to use the
mailx
utility.
If you wish to send the contents of a file as the body of a message, you can use a command like the following one:
$ mailx someone@example.com <report.txt
The above command would send the contents of the file report.txt
to someone@example.com. The contents would be inserted within the body of
the message. In the above case, the message would have no subject. You can
use the -s
option to the command to provide a subject for the
message. E.g.:
$ mailx -s "Just a test" someone@example.com <report.txt
The above command would send the same message as the previous one, but this time with a subject line of "Just a test". If the subject line contains a space, you must enclose the subject within quotes.
If, instead of sending a file's contents in the body of the message, you
wish to send it as an attchment, you can use the -a
option on
Linux/Unix systems, but not on OS X, since the -a
option isn't
supported for mailx for that operating system.
$ mailx -s "another test" -a report.txt -b someoneelse@example2.com,jdoe@anothersite.com someone@example.com A test message with an attachment. Also sending to two BCC email addresses EOT
In the above example, the file report.txt
is sent as an
attachment to the message with someone@example.com used as the "to" address.
I also sent the message to two additional addresses
as blind carbon copy (BCC) addreses using the -b
option. When
using the -b
option to send to multiple email addresses,
separate the addresses with a comma. Instead of putting the contents of the
file report.txt
in the body, the body in the above example
consists of the two lines starting with "A test message". You can provide
the body of the message by simply typing the mailx command line then hitting
return and then starting typing whatever you want to appear in the body of
the message. When you are finished, hit Ctrl-D
, i.e., the
Ctrl
and D
keys simultaneously. You will then see
EOT
appear for the end of text and the message will be transmitted.
If you wish to put email addresses in the carbon copy (CC) field, use
-c
, instead of -b
. As with the -b
option,
separate multiple email addresses with a comma.
If you wish to see if the message has been transmitted from the sending
system, you can use the mailq
command. If it hasn't been
transmitted you will see the message still in the queue as in the example
below from a Mac OS X system.
$ mailq -Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient------- AEADBD2536AA* 370 Fri Apr 10 22:10:44 jsmith@GSOD000962737L.local someone@example.com someoneelse@example2.com
On a Linux/Unix system, you will need to run the mailq command as root or you can run the command with sudo, if the account you are using is in the sudoers file.
Though, if the message isn't shown in the queue, there still could have been a failed delivery attempt. E.g., a short time after issuing the mailq command above on a OS X system, when I reissued the command I saw the following indicating the system could not send the email via mailx.
$ mailq postqueue: fatal: Queue report unavailable - mail system is down