Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm running centos and have a rediculous amount of emails in the mailqueue due to a script error. Is there any way to remove messages that have a certain subject line in sendmail's mailqueue.

share|improve this question
up vote 4 down vote accepted

This worked for me just now, in testing (which is why it has such a weird subject line embedded in the code). It relies on the fact that the two files that make up a message in sendmail queue share the same filename except for the first character.

cd /var/spool/mqueue
for file in `grep "Subject:.*litigate gasworks" *|cut -f 1 -d:|cut -c2-`; do echo *${file} ; done

Once you're happy with the list of files it produces, change echo to rm. Don't forget to stop sendmail before you mess with its queue like this; it may become surly if the queue changes underneath it.

share|improve this answer

In the sendmail contrib/ directory there's a perl script qtool.pl that lets you safely make interventions on spool directories while sendmail is running. You can delete, bounce, move, and filter by age, size, sender, recipient and subject (amongst other things).

Inexplicably, it is not included with the main CentOS sendmail package, you can find it in the sendmail-doc rpm though (thanks to kubanczyk for noting this), or you can extract it from the source distribution ( ftp://ftp.sendmail.org/pub/sendmail/sendmail-current.tar.gz ). It has minimal dependencies, they should all be part of a normal perl installation.

To delete messages use -d:

qtool.pl -d -e '($msg{headers}->{Subject} =~ /whatever you need/i)' /var/spool/mqueue

To move them to an offline mqueue directory (safer):

mkdir /var/spool/mqueue-junk
qtool.pl -e '($msg{headers}->{Subject} =~ /whatever you need/i)' \
    /var/spool/mqueue-junk /var/spool/mqueue

Note it's "qtool.pl destination/ source/". Locked files (those with a "*" by the queue ID in mailq output) will not be moved, you may need to retry to catch those.

You have the option of running that queue later with sendmail -qR [...] -O QueueDirectory=/var/spool/mqueue-junk .

(Older versions of qtool.pl are known to not parse the headers correctly, you need to omit the first letter on most headers, e.g. $msg{headers}->{ubject} instead of {Subject} )

share|improve this answer
1  
qtool.pl is included in CentOS sendmail-doc rpm. – kubanczyk Feb 18 at 18:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.