mail

Remove stuck emails from postfix mail queue

If there are only one or two emails which should be removed, you can just use their Queue ID from the mailq(1) output and delete them with the following command.

$ postsuper -d CB7E5B0067
postsuper: CB7E5B0067: removed
postsuper: Deleted: 1 messages

The postsuper(1) command will delete the message with the queue ID “CB7E5B0067” from the queue.

sorting through the email queue

A typical mailq output will look similar to this.

-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
E2AE38F3712     4883 Mon Nov 27 20:45:11  user@example.com
(delivery temporarily suspended: lost connection with mx3.qq.com[123.123.123.123] while sending MAIL FROM)
                                         1506487969@qq.com

E584DB45F5     4337 Mon Nov 27 17:01:48  user@example.com
(delivery temporarily suspended: lost connection with mx3.qq.com[123.123.123.123] while sending MAIL FROM)
                                         631676778@qq.com

-- 638 Kbytes in 2 Requests.

The following command, described in detail below, will format the mailq output, filter the list for the string “user@example.com” and delete the associated messages from the queue.

$ mailq | grep -v '^\-' | awk '/^$/{print ""}{printf $0" "}' | grep 'user@example\.com' | awk '{gsub(/!|\*/, "", $1); print $1}' | postsuper -d -

Test before you delete

$ mailq | grep -v '^\-' | awk '/^$/{print ""}{printf $0" "}' | grep 'user@example\.com'

Blocking emails from single sender

With postfix, there is the possibility to use the “check_sender_access” to set individual actions per sender address. For the list of recipients and their action, a mapping needs to be created in the form of a hash database file.

create hash database file

$ vim /etc/postfix/sender_access

add spamer

# ADDRESS PATTERNS         # ACTION
sender@example.com         550 Blacklisted
domain.com                 REJECT
user@                      REJECT

possible actions can be found in the postfix man page access(5) under the section “REJECT ACTIONS”.

generate hash db

$ postmap /etc/postfix/sender_access
$ ls -1 /etc/postfix/sender_access*
/etc/postfix/sender_access
/etc/postfix/sender_access.db

add definitino to main.cf

smtpd_recipient_restrictions = check_sender_access hash:/etc/postfix/sender_access,...

more great tips for postfix here