Linux Training

Linux training for private, public & voluntary sector.

0800 024 8425

City LinUX sample scripts - chkmail


chkmail - checks the availability of a mail account on a remote IMAP server and optionally prints and or logs the number of messages in the nominated mailboxes. A warning threshold may be set. The mailbox user and/or administrators may be advised if the number of messages exceeds the threshold set.


chkmail [ -a <mail_addr ] [ -b <mail_box> ] [ -c <config_file> ] [ -d ] [ -i <imap_server> ] [ -L ] [ -l ] [ -m <#max> ] [ -n ] [ -P <passwd> ] [ -p <port_no> ] [ -u <login_name> ] [ -v ] [ config_id>|"ALL" ] | [ -V ]


chkmail is a Bourne shell script which should work equally well on all versions of UNIX, Linux and Mac OS X.


chkmail uses " telnet " and " expect " to connect to an IMAP server and examine nominated mailboxes. The utility optionally prints the number of messages in each specified mailbox. The script can log the presence of oversize mailboxes, advise the owners of the mailbox regarding the need for good house keeping and/or purge the excess mail.

I wrote this one after I discovered that a client who was complaining about POP3 performance on a 1 and 1 server in the cloud, had over 20,000 messages in her in-boxes, 13.5 thousand of them in a single mailbox. Nothing was ever being deleted or archived, not even spam. Manipulating the mailboxes through the Mac-OS X mail client was a nightmare. I needed a tool that would monitor the the remote mailboxes and return data that I could feed into alerts and e-mail reminders. NB . A short client id can be given which makes it easy to use in an ad hoc fashion from the terminal. The id is used to look up the details of the mail account in the configuration file.


-a <mail_address> In these days of virtual mail boxes the users mail address is usually the login name for the IMAP mail account. The Unix login name is still used where the user has an account on the box (well it is on my boxes) and this can be given here or sent with the -u    option below. NB The command line options supercede the configuration file settings.

-b <mailbox> Count the number of messages in the nominated mailboxes. The option is redundant if the mailbox id is used.

-c    Use the specified configuration file in preference to the default "/usr/local/etc/" . If no configuration file is found the script will attempt to soldier on with whatever information was supplied from the command line. If all else fails chkmail will attempt to check " INBOX" for the user invoking the script. The IMAP server name being constructed from the local domain ( hostname -d ). It would be more sensible to terminate with syntax message on standard error but this does make some sort of sense if like me you want a lazy way to count the messages in your own in-box.

-d    Set debug mode on. The -x    option is set within the shell and each command in the script is echoed to the console.

-i <imap_server_name> . The name or IP address of the server to which the expect script should connect.

-L    List the available mailboxes for this mail user account.

-l    Use the local syslog facility to log that the check has been made and record the number of messages returned.

-m    <#_max_no_of messages> Sets the threshold for the number of messages a mail box may hold before a warning is issued. If the -n    option is used warning message will be sent by e-mail to the mail box owner or the person defined in the configuration file with the alert equate, a warning is also printed to standard error. The warning threshold may also be set in the configuration file but will be over ridden by the value set here.

-n    Notify the user if they have exceeded the warning threshold for the number of messages in their mail box (see -m    option below).

-P <password> . The password for the mail user account. It is possible to use this option in combination with the configuration file if you don't want to store passwords on disc.

-p <port_number> . The ports that chkmail knows how to handle are 143, 993 and 22. If port 143 is used chkmail will access the mail account using expect and telnet to send the appropriate IMAP commands. If port 993 is selected chkmail will try to use openssl with expect. Use of both ports 143 and 993 will be relatively slow and require knowledge of the mail account password which may be passed on the command line or stored in Using these options does however have the advantage that no knowledge of the back end mail store architecture is required. Using 22 ( ssh ) has the advantage of being much quicker and the user only requires sudo root access. At present however only Courier mailboxes, as configured by Plesk under CentOS 6, are supported.

-s    Force the script to use the imap protocol to contact the mail host.

-u <username> . The name for the IMAP account login. In reality this duplicates the -a    option but it just seemed more intuitive to have both.

-v    Set verbose mode on. Without this option chkmail reports only on those mail boxes where the number of messages either equals or exceeds the trigger value for warnings. With the -v    option chkmail always reports the checking of a mail box and the number of messages found.

< config_id|"ALL" > The configuration identity is the key to the stanza in the configuration file which will be used to determine the values to be used by the script. The configuration equates recognised by the scripts are:

addr      Email address to be checked.
imap      IMAP server name or IP.
passwd    Password for the email account.
alert     Recipient address for alerts.
warn      Trigger value for e-mail alerts.
folders   The mailbox folders to be checked.

The id "ALL" is special and causes chkmail to scan the chkmail file to compile a list of all the available ids and to check each one in turn.

-V    Print the version details and exit.

FILES, msg_cnt, msg_del.


chkmail flash

The id "flash" is looked up in the default configuration file /usr/local/etc/ . Using the e-mail account name and password found there, the script calls an expect script "/usr/local/etc/msg_cnt" to login to the IMAP server and get a count of the messages in the nominated mailbox. If the number of messages exceeds the default of 100 a warning is printed to screen.

chkmail -a \\

-i -l -p password -m INBOX.Spam

No configuration id has been given so chkmail will only use the command line options. An IMAP login will be attempted on using the user name and the password "password" as given on the command line. The check will be performed on the folder "INBOX.Spam"and the total number of messages returned will be logged using the local syslog facility. A warning will be printed to screen if the folder holds more than the default value of 100.

chkmail  -v -p 993 -m 1000 -x -n dermot 

The script will lookup the details for " dermot " in the default configuration file /usr/local/etc/ The account will be accessed using SSL (secure sockets layer) and the INBOX, together with any folders specified in the stanza labelled dermot, will be checked and the number of messages counted. If there are more than 1000 messages in any folder being checked, the nominated person (which may or may not be the account holder) will be notified and requested to ensure that excess messages are removed. The -x option will also ensure that messages in excess of 1000 will be deleted.


The script is quite crude and has very little, if any, error checking. The consequences of unexpected input are untested.


chkdf, chkftpd, chkfw, chkup, msg_cnt, msg_del.


Clifford W Fulford, City Linux. Contact or +44 (0)709 229 5385.

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.