Daily testing of email deliveries using mailx
I needed a way to perform a daily check that a Linux email server is able to
successfully transmit email to external email addresses. Such a test can
easily be scheduled using
cron and
mailx. You can use the crontab utility to schedule
mailx to run periodically and send a test message to a specified email address.
E.g., the following entry will send a test message at five minutes after noon
every day to john.doe@example.com:
05 12 * * * mailx -s "Daily email delivery test" john.doe@example.com </home/jan/Documents/daily_mail_test_message.txt
The body of the message will contain the contents of the file
/home/jan/Documents/daily_mail_test_message.txt
.
The first 5 elements on the line in the crontab file are scheduling
elements:
# ┌───────────── min (0 - 59)
# │ ┌────────────── hour (0 - 23)
# │ │ ┌─────────────── day of month (1 - 31)
# │ │ │ ┌──────────────── month (1 - 12)
# │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
# │ │ │ │ │ Saturday, or use names; 7 is also Sunday)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute
If an asterisk is used for an element, that indicates that scheduling is
done for every possible value for that element. E.g., the 05 on the line
indicates five minutes after the hour, in this case 12 (noon) and the following
three asterisks indicate that the cron job should be run every day of every
month and every day of the week, i.e., Sunday to Saturday.
[ More Info ]
[/network/email/mailx]
permanent link
Sending messages and files with mailx
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
[/network/email/mailx]
permanent link