December 2012
« Nov   Jan »

Qmail : Removing emails from queue having the specified Subject

One system has mis-configured to send tons of emails though an application and few 1000 emails are already had added in queue along with other genuine emails. So my task is to remove theses unwanted emails which containing the specified subject.

my email server is Qmail which is an excellent mail server for sending bulk emails. I see that it’s email delivery rate and reliability is quite same as on Postfix MTA.

Here are the steps,

a. Identify the email header id from the queued emails.. I’m goingg to use “qmail-qstat” command to know the email header. ID will starts with # symbal eg. #1445826

 [root@mail4 ~]# /var/qmail/bin/qmail-qread  | more
4 Dec 2012 17:04:33 GMT  #1442054  7146  <>
4 Dec 2012 17:06:16 GMT  #1442974  7048  <>
4 Dec 2012 17:08:47 GMT  #1444078  7064  <>

b. Find the messages associated with this email header.

 [root@mail4 ~]# find /var/qmail/queue -iname 1445826

Now open the file to read the email body. Choose the file part contains message (/var/qmail/queue/mess/0/1445826)

[root@mail4 ~]# cat /var/qmail/queue/mess/0/1445826 | more
Received: (qmail 20473 invoked by uid 0); 4 Dec 2012 17:12:24 -0000
Date: 4 Dec 2012 17:12:24 -0000
Message-ID: <>
Subject: Better than EQP
From: Uniline Promo Inc <>
Reply-To: Uniline Promo Inc <>
MIME-Version: 1.0
Content-type: multipart/alternative; boundary="_d96476de58f15ff5cda2ac8a00860156"
This is a multi-part message in MIME format.
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Now you can see the email subject from the email message part. You may need to install another third party tool to remove the email having the specified the Subject from the queue.

C. Install qmail-remove script
Download this package from

#tar -zxvf mail-remove-0.95.tar.gz
#cd mail-remove-0.95
#make && make install
#ln -s /usr/local/sbin/qmail-remove /usr/bin/qmail-remove
#mkdir -p /var/qmail/queue/yanked/

/var/qmail/queue/yanked/ –> This folder would have store all the files those are moved through this script.

D. Delete the specified emails having the same subject

#/etc/init.d/qmail stop  
#qmail-remove -r -p "email subject name"

How to check the email sending statistics

[root@mail4 yanked]# /var/qmail/bin/qmail-qstat
messages in queue: 30835
messages in queue but not yet preprocessed: 230
[root@mail4 yanked]#

If you want to remove all the email from a particular user “”, just execute the command as shown below

[root@rc-025 test]#qmail-remove -r -p ""

Also I found another useful tool (qmHandle) for listing the email headers including sender,receiver address along with the email subject. So that we can identify the whether the email is spam or not and also we can use these information for pattern match deletion.

[root@rc-025 test]#
[root@rc-025 test]# tar -zxvf qmhandle-1.3.2.tar.gz && cd qmhandle-1.3.2
[root@rc-025 qmhandle-1.3.2]# cp qmHandle /usr/bin

How to list the Queued emails

[root@rc-025 test]# qmHandle -R
402725179 (9, 9/402725179)
  From: Kathy Keeton <>
  To: "" <>
  Subject: Cheer registration
  Date: Wed, 3 Apr 2013 08:42:18 -0400
  Size: 1147 bytes
Total messages: 1
Messages with local recipients: 0
Messages with remote recipients: 1
Messages with bounces: 0
Messages in preprocess: 0

Reviewing the queued emails [How to store all queued email informations before delete]

[root@rc-025 test]#qmHandle -R > /tmp/remotequeue.txt

Unfortunately I could not get success to delete queued emails using qmHandle as it’s showing some error in return.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>