Qmail

From Server Knowledge Base
Jump to navigationJump to search

As a service

/etc/init.d/qmail start|restart|stop

Mail in the queue

/var/qmail/bin/qmail-qstat

Resend it: qmailctl doqueue

  • See how many times a user is mentioned in the mail log: cat mail.log | find [email protected] | wc -l

- e.g. cat /opt/psa/var/log/maillog | find [email protected] | wc -l

  • Find mail from or to a user in mail log: cat /opt/psa/var/log/maillog | grep [email protected] -C 15
  • Find emails over 1MB: find /var/qmail/queue/ -type f -size +1000000 -a -print -exec ls -l {} \;
  • Find mail from or to a user in the queue: find /var/qmail/queue/ -type f -exec grep -l [email protected] {} \;

Remove ones with @gmail.com in them: find /var/qmail/queue/ -type f -exec grep -l '@gmail.com' {} \; | xargs rm
Remove failed emails: find /var/qmail/queue/ -type f -exec grep -l 'Subject: failure notice' {} \; | xargs rm

A good method to scan the mail log if you know when the mail was sent is to do:

tac /opt/psa/var/log/maillog
CTRL + C
Right click Putty "Copy All to Clipboard"
Paste into Notepad
CTRL + F [email protected]

Or do cp mail.log /var/www/vhosts/domain.com/httpdocs Download it via FTP and view it in notepad

Main mail log

/opt/psa/var/log/maillog
/usr/local/psa/var/log/maillog
/var/log/maillog

opt is a symlink for /usr/local. This file is good for checking failed login attempts and failed/successful emails.

Mailnames is the individual users/mailboxes. To test sent mail from an external command line type telnet VPS IP 25 or on the server telnet localhost 25. If it does not return 220 or any lines, it may be set in the firewall within Modules in Plesk.

Qmail-queue not processing outgoing or incoming mail example

Mail Log messages

delivery 1: deferral: 92.60.112.25_failed_on_DATA_command./Remote_host_said:_451_4.7.1_Please_try_again_later

This normally happens when a recipient has you on a greylist. The mail may take a while to come through. Try sending to another recipient to see if it is any quicker.

"501 Syntactically invalid HELO argument(s)

Change your hostname to a valid entry.

Increasing mail sending/attachment limit

vim /var/qmail/control/databytes
10485760 #for 10MB
#20971520 #for 20MB

Removing mail via Qmail

Stop qmail first before doing this command using /etc/init.d/qmail stop

/usr/local/psa/admin/bin/mailqueuemng -D
/usr/local/psa/admin/bin/mailmng --help

Removing mail from Qmail manually

To be safe do this first:

tar -zcvf /root/qmail-queue-backup.tar.gz /var/qmail/queue/mess/
/etc/init.d/qmail stop ; svc -d /service/qmail-qmqpd ; svc -d /service/qmail-send ; svc -d /service/qmail-smtpd ; /etc/init.d/qmail stop
telnet localhost 25

If you can still telnet to 25, check if qmail is still running and kill the process and run the above commands again:

ps aux | grep qmail

cd /var/qmail/bin
find /var/qmail/queue/mess/ -type f -mtime +1 -exec rm -v {} \;

This removes mail from before the last 48 hours.

find /var/qmail/queue/mess/ -type f -mtime 1 -exec rm -v {} \;

This removes mail from the last 24-48 hours.

./ubu-queue-fix /var/qmail/queue/

CTRL + R allows you to reverse search your bash history.

Removing mail via qmHandle

If you are going to do this you have to stop qmail using either:

/etc/init.d/qmail stop

or

svc -d /service/qmail-qmqpd ; svc -d /service/qmail-send ; svc -d /service/qmail-smtpd ; /etc/init.d/qmail stop
wget serverkb.co.uk/tools/qmhandle.tar.gz
gunzip qmhandle.tar.gz ; tar -xvf qmhandle.tar ; cd qmhandle-1.3.2
./qmHandle
qmHandle -c -s
qmHandle -c -l
qmHandle -c -m(mail ID)

To delete one email find the number next to the hash) and do:

/var/qmail/bin/qmail-qread
./qmHandle -d<mailid>

To delete the entire queue do;

qmHandle -D

Then start qmail again using either:

/etc/init.d/qmail start

or

svc -u /service/qmail-*

Resend remote queue

Or /etc/init.d/qmail doqueue (commonly won't be there)

Installing qmqtool

wget http://jeremy.kister.net/code/qmqtool/files/qmqtool-1.14.tgz
tar –xpf /path/to/qmqtool-1.14.tgz

Once the .tgz is extracted, check the following installation. qmqtool is just a lightweight perl script. Without the ./configure process, users are left changing the script (/usr/local/bin/perl vs. /usr/bin/perl, etc.). The install process:

1. ./configure
   optional arguments to configure are:
   --prefix=PREFIX   install architecture-independent files in PREFIX
                      [/usr/local]
   --mandir=DIR      man documentation [PREFIX/man]
   --scriptdir=DIR   qmqtool script [PREFIX/script]

   --perl=PERL       full path to perl (e.g. /usr/local/bin/perl)
   --qmaildir=DIR    directory qmail is locted (e.g. /var/qmail)
   --upgrade         upgrade existing installation
                      (default when no prefix/mandir/scriptdir specified)
   --help            show help message

2. make
3. make install

Important files

/var/qmail/control/rcpthosts
/var/qmail/control/me
/var/qmail/control/virtualdomains
/var/qmail/control/locals

The default value in /var/qmail/control/concurrencyremote is 20

Script to email if queue exceeds a limit

#!/bin/bash
ATTACH="/root/mailcount.txt"
EMAIL="[email protected]"
LIMIT=300
QSTAT="/var/qmail/bin/qmail-qstat"
QREAD="/var/qmail/bin/qmail-qread"
QVALUE=`/var/qmail/bin/qmail-qstat | head -1 | awk '{print $4;}'`
WARNING="Your mail queue is over $LIMIT, please investigate."

$QSTAT | head -1
#this one below is used for when the queue is below the limit
$QSTAT | head -1 > /root/mailqueue.txt
$QREAD | head -100
#these two below append the amount in queue and the headers, and is used when the queue is above the limit
$QSTAT | head -1 >> /root/mailcount.txt
$QREAD | head -100 >> /root/mailcount.txt

if
 [ "$QVALUE" -ge "$LIMIT" ] ; then
 echo $WARNING
 mail -s"$WARNING" $EMAIL < $ATTACH
  else

 if
  [ "$QVALUE" -lt "$LIMIT" ] ; then
 #this ensures it only sends the mail queue amount if the queue is below 300, instead of the qread output
 if [ -f $ATTACH ] ; then
  echo "$ATTACH exists, removed."
  rm /root/mailcount.txt
 fi

 echo Mail Queue below $LIMIT
 mail -s"Mail Queue count" $EMAIL < /root/mailqueue.txt
 fi
fi

if [ -f $ATTACH ]
 then
  echo "$ATTACH exists, removed."
  rm /root/mailcount.txt
fi

if [ -f /root/mailqueue.txt ]
 then
  echo "/root/mailqueue.txt exists, removed."
  rm /root/mailqueue.txt
fi

Stop qmail listening on port 2500

To stop qmail running/listening on port 2500 do:

svc -d /service/qmail-smtpd-2500
svc -d /service/qmail-smtpd-2500/log
rm -r /service/qmail-smtpd-2500

Correct permissions

This is important as if you escalate permissions to 777 mail will fail and you will get one of the following messages in your maillog or bouncebacks:

delivery 54: deferral: Uh-oh:_home_directory_is_writable._(#4.7.0)/
  • qmail folder and the folders below should be 755 (rwxr-x-rx):
    • alias, bin, boot, control, plugins, popuser, queue, users and mailnames
    • alias should have alias:qmail, bin, boot, control, plugins, users should have root:qmail
      • queue folder should be owned by qmailq:qmail
      • mailnames should be owned by root:root and all domain folders below have 700 (rwx------)
  • All domain folders e.g. example.co.uk below mailnames should be owned by popuser:popuser (the folder popuser needs popuser:popuser)
    • Within a domain folder .qmail-default should have 600 (rw-------) and the user account folders should have 700, for example:
      • admin
      • postmaster
        • Within the user account folder .qmail should have 600, .spamassassin should have 755 and @attachments and Maildir should have 700
          • Within .spamassassin:
            • user_prefs should have 600
          • Within Maildir:
            • courierimapsubscribed, courierimapuiddb and maildirsize should have 644 (rw-r--r--)
            • courierimapkeywords, cur, new, tmp, .Sent and .Spam should have 700. Within .Sent and .Spam:
              • maildirfolder should have 600
              • courierimapacl, courierimapuiddb should have 644
              • courierimapkeywords, cur, new and tmp should have 700

If you get the below error on a Plesk server, increase this or set the Subscription to Unlimited.

qq trouble in home directory (#4.3.0) (state 17).