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
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 firstname.lastname@example.org <report.txt
The above command would send the contents of the file
to email@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
-s option to the command to provide a subject for the
$ mailx -s "Just a test" firstname.lastname@example.org <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 email@example.com,firstname.lastname@example.org email@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 firstname.lastname@example.org 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
-b option to send to multiple email addresses,
separate the addresses with a comma. Instead of putting the contents of the
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
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
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 email@example.com firstname.lastname@example.org
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