Skip to content

How to remove undeliverable SPAM messages from Qmail Mail Server using qmHandle

As a website design and development agency, we are inextricably tied to the world of servers, email and the bane of everyone’s existence, SPAM (no, not the canned meat – you know the one). Whilst many websites operate on shared hosting systems, and the wildly popular cPanel platform, we’re seeing a growing number of dedicated or virtual servers in operation. Maybe you, like me, thought self-hosted email solutions were a dying technology with Gmail and Office 365 being available ubiquitously. But no – old, physical machines are being virtualised and we’re seeing a surprising number of older systems standing the test of time. Take Qmail for example, a Mail Transfer Agent (MTA) designed to replace sendmail back in 1995. We have happened across a few environments where Qmail is in use because “it’s simple and just works”. We can’t argue with that, but if you’ve ever found yourself navigating SPAM and Qmail, you may have come across an issue where some messages aren’t being delivered.

 

If you’re running a Qmail mail server that’s been hit by some SPAM, chances are it’s just sitting in the queue. You could just clear the entire queue, but to be safe (if there is some consistency to the SPAM. I.e. the same recipient or sender address), then you can run a filtered deletion command using qmHandle. The package isn’t installed by default, so follow the steps below to download and use qmHandle:

  1. Login onto the mail server and download the qmHandle package using the link below:-cd /usr/local/src/
    wget https://sourceforge.net/projects/qmhandle/files/latest/download
    [EDIT: I actually hit a certificate error the second time I ran this (on a different server). I was able to proceed by appending the “–no-check-certificate” parameter.]
  2. Extract the qmHandle binary using the command below:-tar -xzf download OR qmhandle-1.3.2.tar.gz
  3. Move the Qmail binary from its directory to the Qmail binary directory where you’ve installed it on the server. For example if you installed it under /var/qmail/ follow the steps below:cd qmhandle-*/
    mv qmHandle /var/qmail/bin/
  4. Now that qmHandle is set up, we’ll use it to check for message from a certain sender, and delete these messages:/var/qmail/bin/qmHandle -l | grep From | grep FROM_ADDRESS
    qmailctl stop
    /var/qmail/bin/qmHandle –fFROM_ADDRESS

      • [EDIT: On my second run I had to vi the qmHandle script to change the stopqmail and startqmail variables. I left stopqmail blank, and set startqmail to the <1.03 version setting.]
      qmailctl start

Here are a list of other commands for qmHandle:

# Forcefully send emails.
qmHandle -a

# List message queues
qmHandle -l

# delete all messages in the queue
qmHandle -D

# list local message queue
qmHandle -L

# list remote message queue
qmHandle -R

# display message number N
qmHandle -mN

# delete message number N
qmHandle -dN

# delete all messages that contain text as Subject
qmHandle -Stext

# delete all messages that contain text as From email address
qmHandle -Ftext

# delete all messages that contain text as To email address
qmHandle -Ttext

 

In 2020, there are a seemingly unlimited number of server + email server combinations. If you’re dealing with a Linux/Unix environment running Qmail to handle mail, then hopefully you can now deal with this pesky little issue!

Further Resources:

http://www.jodohost.com/blog/entry/how-to-install-and-use-qmhandle-utility-with-qmail-/

Manage the ‘qmail’ message queue using qmHandle