Ramblings from MostlyChris

Tech stuff and a bit more

Browsing Posts in Qmail

Tonight I learned something. Yep! Still learning after all this time. What did I learn? Well I'm glad you asked. I had an occasion to have to delete all emails that were marked as spam but only for accounts that were forwarded to another email address.

The players:
RHEL 4
Plesk
Qmail

The magic:

for i in `find ./ -name .qmail | xargs grep '^\&' | cut -d: -f1 | sort | uniq `; do grep psa-spam
c $i > /dev/null; if [[ $? -eq "1" ]]; then sed -i '1i | /usr/local/psa/bin/psa-spamc reject' $i; fi; done

That's all there is to it. Wait? What? You don't understand? Well let me break it down for you. Actually, let me summarize for you. Time constraints don't permit me to write all day on this.

The one-liner above finds all .qmail files that have a forward email address in them. The forward address is denoted by a '&' at the beginning of a line. The cut command then strips off the beginning of the line and only shows the name of the file and the directory it is in. Since it is possible that there may be more than one line with '&' in the .qmail file, we sort uniq so that we don't enter the psa-spamc line more than once. Next, we use grep to check for the line '/usr/local/psa/bin/psa-spamc reject'. If that line already exists, we don't want to add it again. Finally, we use 'sed' to place that line at the top of the .qmail file. That's it. All done!

Suppose you would like to send a copy of an incoming email to another account while the email still goes to the original account. You can do this by modifying the .qmail file of the original recipient and adding an entry that looks like the following:

&user@domain.com

On a Plesk server, a full .qmail file with the above changes looks like this:

| true
&user@domain.com
| /usr/bin/deliverquota ./Maildir

Keep in mind that if you do this on a Plesk server, these changes will be overwritten with mchk or upgrades so you will have to take measures to deal with this. I set the files immutable so they can't be changed by Plesk. This causes its own problems so you will have to decide if it is worth it.

I may be wrong here, but I believe the default queue lifetime for a message sitting in the qmail queue is 7 days. To me, that's entirely too long to wait to deliver a defunct message considering most of them are probably SPAM bounces anyway.

Here is how to shorten the lifetime.

Change to the qmail control directory, assuming you use default locations on your server.

cd /var/qmail/control

Set the timeout to the number of seconds you want the message to remain in the queue. This is done in the queuelifetime file. This file doesn't normally exist by default. I'll use 24 hours (86,400 seconds) as an example.

echo "86400" > queuelifetime

Restart qmail.

/etc/init.d/qmail restart

That's it. Messages that don't get delivered in 24 hours will be gone.

First, copy the smtp_psa file.

cp /etc/xinetd.d/smtp_psa /etc/xinetd.d/smtp_additional

Edit the newly copied file and replace the first line. Replace 'service smtp' with 'service submission'

vi /etc/xinetd.d/smtp_additional

Restart xinetd.d

service xinetd restart

Check that port 587 is listening on the server

netstat -ltnp

qmHandle is a handy tool to aid in administering the queue on qmail enabled servers.

Get and install qmHandle

wget http://ns.nywtc.co.uk/qmHandle && chmod +x qmHandle && cp -a qmHandle /usr/local/bin/

Get and replace existing qmHandle

wget http://ns.nywtc.co.uk/qmHandle && chmod +x qmHandle && cp qmHandle `which qmHandle`

Usage is simple. After installing, just issue qmHandle without any arguments and you will be presented with a list of options.