In this article, we will see how we can get a high mail queue alert notification in our Carbonio CE email server.
Why Do We Need To Be Careful With Mail Queue
Imagine one of your email accounts got compromised and sending huge amount of unsolicited emails (spam mails). What will happen? At the start your server will forward all spam mails. As the number of generated spam mails increases the server will take more time to process each mail hence, the mail queue will grow rapidly. Meanwhile, your server’s mail transaction will get stuck and your server’s IP/domain reputation will take huge blow. let’s take a look at below scenario:
What if, you get a notification of high mail queue at the earliest? You can stop the spamming right? Moreover, you can save your server’s reputation.
Unless you have any paid mechanism (Monitoring tool, email gateway etc.) how could you achieve this on your own?
Usually, a server clears the mail queue instantly. But often mails got queued for various reasons:
- Your server is unable to connect to the remote end server.
- Your server is not been able to resolve DNS information of remote end server.
- Recipient’s mailbox is full
And as a system admin you may have faced some weird scenarios when the mail got queued. So the scenario would be like:
So the point of all these examples are to realize that if we get a notification about something unusual in our mail queue, we could prevent many unwanted situations. As a sysadmin, you are free to explore any solution but we thought this article could help you in this cause.
How Mail Queue Looks Like In This Scenario
We have set up a Carbonio CE server. Currently, it has 6 emails in it’s mail queue. So it looks like,
zextras@mail:~$ mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
A49251E0B5D 2198 Mon Sep 19 10:53:52 sharif@example.com
(host mail.example.com[192.168.1.153] said: 450 4.2.1 Mailbox disabled, not accepting messages (in reply to RCPT TO com mand))
flaminia@example.com
368361E0CA0 2200 Mon Sep 19 10:58:03 suman@example.com
(host mail.example.com[192.168.1.153] said: 450 4.2.1 Mailbox disabled, not accepting messages (in reply to RCPT TO com mand))
flaminia@example.com
1A1CA1E0CA3 2204 Mon Sep 19 10:59:06 stefania@example.com
(host mail.example.com[192.168.1.153] said: 450 4.2.1 Mailbox disabled, not accepting messages (in reply to RCPT TO com mand))
flaminia@example.com
CDD5E1E0C67 2201 Mon Sep 19 10:56:31 irene@example.com
(host mail.example.com[192.168.1.153] said: 450 4.2.1 Mailbox disabled, not accepting messages (in reply to RCPT TO com mand))
flaminia@example.com
788391E0B5F 2205 Mon Sep 19 10:54:12 sharif@example.com
(host mail.example.com[192.168.1.153] said: 450 4.2.1 Mailbox disabled, not accepting messages (in reply to RCPT TO com mand))
flaminia@example.com
7342D1E0C6F 2198 Mon Sep 19 10:57:11 irene@example.com
(host mail.example.com[192.168.1.153] said: 450 4.2.1 Mailbox disabled, not accepting messages (in reply to RCPT TO com mand))
flaminia@example.com
-- 12 Kbytes in 6 Requests.
zextras@mail:~$
Generally, server does not bother about how many emails are in the mail queue, but in the long run it affects your email communication.
Script For Mail Queue Status Notification
Now we will set a script in our server with a threshold value of 5 so that whenever mail queue exceeds 5 our designated email account will get a notification. This threshold value is customizable.
[Create this script in the server]
#!/bin/bash
mailq_count="$(/opt/zextras/common/sbin/mailq | /usr/bin/tail -n1 | /usr/bin/gawk '{print $5}')"
mailq_status="$(su - zextras -c mailq|grep ^[A-F0-9]|cut -c 42-80|sort |uniq -c|sort -n|tail)"
DOM=example.com
SERVER=mail.$DOM
FROM=zextras@$DOM
TO=arman@example.com
# If variable is empty, then the queue is empty -> set it to zero
if [ -z "$mailq_count" ]; then
mailq_count=0
fi
############################################################################
if [ "$mailq_count" -gt 5 ]; then
/opt/zextras/common/sbin/sendmail -i $FROM $TO <<MAIL_END
Subject: Mail queue || mail.$DOM
FROM: $FROM
To: $TO
Mail queue count on mail.$DOM ${mailq_count}
$SERVER
***********************
$mailq_status
MAIL_END
fi
Make this script executable.
root@mail:/home/carbonio# chmod +x /root/mailq.sh
Set a cron job to run this script at a regular interval.
root@mail:/home/carbonio# crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/2 * * * * /root/mailq.sh
The End Result
So after waiting for a few minutes, we got a notification in our designated email about the current mail queue status as it is exceeding the threshold value.
There are few things that you should be aware of:
- Settings cron job time too low/ too high can cause some inconvenience. i.e. if you set the cron job time too low, then until you fix the mail queue, you will continuously get notifications, believe me it can be irritating sometimes. Therefore, in that case you have to either fix the mail queue or stop the script. On the other hand if you set the cron job time too high, it is possible that your notification mail may also got stucked between other mails in mail queue.
- Good thing is that the script does not bother you with notifications when the mail queue size is below threshold value.
So that’s it for today.
Have a good day.
😊