{"id":1,"date":"2024-05-25T08:44:57","date_gmt":"2024-05-25T08:44:57","guid":{"rendered":"https:\/\/lokka.ro\/blog\/?p=1"},"modified":"2025-10-16T07:13:14","modified_gmt":"2025-10-16T07:13:14","slug":"5-top-tips-for-reviewing-your-postfix-mail-queue","status":"publish","type":"post","link":"https:\/\/lokka.ro\/blog\/2024\/05\/5-top-tips-for-reviewing-your-postfix-mail-queue\/","title":{"rendered":"5 top tips for reviewing your Postfix mail queue"},"content":{"rendered":"\n<p>When an email fails to send, it ends up in one of two queues within Postfix: pending and deferred.<\/p>\n\n\n\n<p>The pending queue includes all messages you&#8217;ve submitted to Postfix that have yet to be sent out and handed off to the recipient server. The deferred mail queue holds all messages that have soft-failed and need to be retried (temporary failure).<\/p>\n\n\n\n<p>Postfix will try resending messages from the deferred queue at set intervals. This is set to 5 minutes by default, but what you may not know is that this process is fully configurable.<\/p>\n\n\n\n<p>Don&#8217;t worry, you&#8217;re not alone. In our experience, most users don&#8217;t know how to review their message queues. That&#8217;s why we have written this handy primer. By entering any of the following commands, you can easily inspect and manage your Postfix message queues.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to review your Postfix message queues<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1) Display mail queues: deferred and pending<\/h3>\n\n\n\n<p>If you simply wish to review your message queues, enter:<\/p>\n\n\n\n<p><em><code>mailq<\/code><\/em><br><br>or<br><br><em><code>postqueue -p<\/code><\/em><\/p>\n\n\n\n<p>Alternatively, if you need to save the output to a text file, run:<\/p>\n\n\n\n<p><em><code>mailq &gt; mailqueue.txt<\/code><\/em><br><br>or<br><br><em><code>postqueue -p &gt; mailqueue.txt<\/code><\/em><\/p>\n\n\n\n<p>Either of these commands will show the sender, recipients, and ID&nbsp;\u2013&nbsp;<strong>not the message itself<\/strong>. Although the ID will help you track down individual messages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) View message (contents, header, and body) in your Postfix queue<\/strong><\/h3>\n\n\n\n<p>Message IDs are available form the message queue. So, to view a message with the ID&nbsp;<em>XXXXXXX<\/em>, you would enter:<\/p>\n\n\n\n<p><em><code>postcat -vq XXXXXXXXXX<\/code><\/em><\/p>\n\n\n\n<p>Or, to save it in a file, enter:<\/p>\n\n\n\n<p><em>postcat -vq XXXXXXXXXX &gt; emailXXXXXXXXXX.txt<\/em><\/p>\n\n\n\n<p>A useful feature for web servers is to enable&nbsp;<em>mail.add_x_header = on<\/em>&nbsp;in the Postfix configuration.<\/p>\n\n\n\n<p>This will add a header to all outgoing email messages showing the script and user that generated each message. Once enabled this will then add the following extra header to message:<\/p>\n\n\n\n<p><em>X-PHP-Originating-Script: 1001:spamEmailer.php<\/em><\/p>\n\n\n\n<p>In this example,&nbsp;<em>1001<\/em>&nbsp;is the user ID and the&nbsp;<em>spamEmailer.php<\/em>&nbsp;is the script sending the message. This allows you to quickly track down the source of any spam messages sent by your server.<\/p>\n\n\n\n<p>With these commands, you should be able to review your mail queue. You can then ensure intended messages are getting through and are not being rejected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to delete queued mail from the mail queue<\/h2>\n\n\n\n<p>Now that you&#8217;re familiar with the commands that will allow you to review your mail queues, let&#8217;s look at how you can delete messages from your backlog.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Tell Postfix to process the queue now<\/h3>\n\n\n\n<p>By entering the right command, you can force Postfix to immediately attempt to send all queued messages. There are two to choose from:<\/p>\n\n\n\n<p><em>postqueue -f<\/em><br><br>or<br><br><em>postfix flush<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Delete queued mail<\/h3>\n\n\n\n<p>A similar command will allow you to delete all queued messages in Postfix. This includes messages from both the pending and deferred queues:<\/p>\n\n\n\n<p><em>postsuper -d ALL<\/em><\/p>\n\n\n\n<p>So, if you needed to delete all queued messages to or from the domain called spamers.com, you would enter:<\/p>\n\n\n\n<p><em>.\/postfix-delete.pl spamers.com<\/em><\/p>\n\n\n\n<p>Similarly, if you wanted to delete all queued messages that contain the word &#8220;spam&#8221; in the e-mail address, you&#8217;d use the following command:<br><br><em>.\/postfix-delete.pl spam<\/em><\/p>\n\n\n\n<p>To delete all messages from the deferred queue specifically (i.e. only the ones the system intends to retry later), enter:<\/p>\n\n\n\n<p><em>postsuper -d ALL deferred<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3) Selectively delete mail from the queue<\/h3>\n\n\n\n<p>You can also delete specific messages from your mail queues. This is not something that is natively included with the standard Postfix tools, but can be done with a bit of Perl scripting.<\/p>\n\n\n\n<p>NB: This Perl script appears to be free; it&#8217;s all over the Internet. That being said, we couldn&#8217;t find out where it originated or who wrote it. Our thanks go out to them either way!<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">postfix-delete.pl:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$REGEXP = shift || die \"no email-adress given (regexp-style, e.g. bl.*\\@gmail.com)!\";\n\n@data = qx&lt;\/usr\/sbin\/postqueue -p&gt;;\nfor (@data) {\n  if (\/^(\\w+)(\\*|\\!)?\\s\/) {\n     $queue_id = $1;\n  }\n  if($queue_id) {\n    if (\/$REGEXP\/i) {\n      $Q{$queue_id} = 1;\n      $queue_id = \"\";\n    }\n  }\n}\n\n#open(POSTSUPER,\"|cat\") || die \"couldn't open postsuper\" ;\nopen(POSTSUPER,\"|postsuper -d -\") || die \"couldn't open postsuper\" ;\n\nforeach (keys %Q) {\n  print POSTSUPER \"$_\\n\";\n};\nclose(POSTSUPER);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Credits: This post was originally written by Jon Totham for a now-deleted domain. We have restored and enhanced the article to preserve its valuable content.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When an email fails to send, it ends up in one of two queues within Postfix: pending and deferred. The pending queue includes all messages you&#8217;ve submitted to Postfix that have yet to be sent out and handed off to the recipient server. The deferred mail queue holds all messages that have soft-failed and need [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[5,4],"class_list":["post-1","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mail-server","tag-mail-server","tag-postfix"],"jetpack_featured_media_url":"https:\/\/lokka.ro\/blog\/wp-content\/uploads\/2024\/05\/Mailboxes_resized.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":4,"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":13,"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/posts\/1\/revisions\/13"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/media\/11"}],"wp:attachment":[{"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lokka.ro\/blog\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}