Linux Training

Linux training for private, public & voluntary sector.

0793 572 8612

City LinUX sample scripts - delmail

NAME

delmail - deletes excess messages from a specified mail account on a remote IMAP server.

SYNOPSIS

delmail [ -c <config_file> ] [ -b <mailbox> ] [ -d ] [ -f ] [ -i <imap_server> ] [ -L ] [ -l ] [ -m <#max> ] [ -n ] [ -P <passwd> ] [ -p <port_no> ] [ -u <login_name> ] [ -v ] [ config_id> ] | [ -V ]

AVAILABILITY

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

DESCRIPTION

delmail can use " telnet " and " expect " to connect to an IMAP server, examine nominated mailboxes and delete old message in excess of the threshold set on the command line or in the configuration file. This access method is preferred when the mail account is active. The method is essential for Mbox mail formats.

Out of hours setting the -p option to port 22 causes the script to ssh to access the mail server and remove the message files directly. This is quicker and appropriate to a regular overnight cron job, it does however require a Maildir email repository. It will not work with Mbox or other mail formats.

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.

OPTIONS

-a <mail_address>

-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/delmail.cf" . 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 delmail 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 or ssh should connect.

This is only required in the unusual situation where the mail domain cannot be resolved.

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

-w    <#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 that the deletions have taken place by send a message to the mail box specified in the configuration file.

-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. The mail account password is only required when imap is being used (ie -p 143 or -p 993 .

-p <port_number> . The ports that delmail knows how to handle are 143, 993 and 22. If port 143 is used delmail will access the mail account using expect and telnet to send the appropriate IMAP commands. If port 993 is selected delmail 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 delmail.cf. 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 delmail reports only on those mail boxes where the number of messages either equals or exceeds the trigger value for warnings. With the -v    option delmail always reports the checking of a mail box and the number of messages found.

< config_id > 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.

-V    Print the version details and exit.

FILES

delmail.cf, msg_cnt, msg_del.

EXAMPLES

delmail -v -l -m 100 fulford@fulford.net

The id "flash" is looked up in the default configuration file /usr/local/etc/delmail.cf . 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.

delmail -a arthur.pendragon@camelot.co.uk \\

-i imap.mailservice.com -l -p password -m INBOX.Spam

No configuration id has been given so delmail will only use the command line options. An IMAP login will be attempted on imap.mailservice.com using the user name arthur.pendragon@camelot.co.uk 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.

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

The script will look-up the details for " dermot " in the default configuration file /usr/local/etc/delmail.cf. 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.

BUGS

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

SEE ALSO

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

AUTHOR

Clifford W Fulford, City Linux. Contact fulford@fulford.net 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.