The files in /var/spool/postfix/deferred/
are not plain text emails but rather Postfix's internal queue format. These files contain:
- Message metadata (envelope information)
- The actual email content (usually in RFC 5322 format)
- Queue management data
Method 1: Using postcat command
The proper way to inspect these files is using Postfix's built-in postcat
utility:
postcat -q /var/spool/postfix/deferred/[QUEUE_ID]
Example output:
*** ENVELOPE RECORDS deferred/1B2C3D ***
message_size: 1024
...
*** MESSAGE CONTENTS deferred/1B2C3D ***
From: sender@example.com
To: recipient@domain.com
Subject: Test message
...
To process multiple deferred messages programmatically:
#!/bin/bash
QUEUE_DIR="/var/spool/postfix/deferred"
for file in $(find $QUEUE_DIR -type f); do
echo "Processing $file"
postcat -q $file > /tmp/$(basename $file).eml
done
Permission problems: Run commands as root or postfix user:
sudo -u postfix postcat -q /var/spool/postfix/deferred/[ID]
Corrupted queue files: Try reconstructing with:
postsuper -r [QUEUE_ID]
For developers needing to parse queue files directly:
import subprocess
def read_deferred_email(queue_id):
result = subprocess.run(
['postcat', '-q', f'/var/spool/postfix/deferred/{queue_id}'],
capture_output=True,
text=True
)
return result.stdout
print(read_deferred_email('1B2C3D'))
Remember that directly manipulating queue files can cause mail system instability. Always back up the directory before making changes.
When Postfix cannot immediately deliver an email, it stores the message in the deferred queue located at /var/spool/postfix/deferred/
. These files aren't in standard mail format but rather in Postfix's internal queue format.
Here are three reliable methods to inspect deferred emails:
1. Using postcat command
The safest way to view deferred messages is using Postfix's built-in postcat
utility:
# View a single deferred message
sudo postcat -q $(ls /var/spool/postfix/deferred/ | head -1)
# View all deferred messages
for file in /var/spool/postfix/deferred/*; do
echo "=== Message: $file ==="
sudo postcat -q "$file"
echo ""
done
2. Converting to mbox format
To convert deferred emails to readable format:
# Install necessary tools
sudo apt-get install postfix-milter
# Convert and save to mbox
sudo bash -c 'for file in /var/spool/postfix/deferred/*; do
postcat -q "$file" >> /tmp/deferred_mail.mbox
done'
3. Using Python to parse queue files
For programmatic access, you can use this Python script:
import os
from subprocess import check_output
def read_deferred_mail():
deferred_dir = '/var/spool/postfix/deferred/'
for filename in os.listdir(deferred_dir):
if not filename.startswith('.'):
full_path = os.path.join(deferred_dir, filename)
try:
mail_content = check_output(['postcat', '-q', full_path])
print(f"=== Message: {filename} ===")
print(mail_content.decode('utf-8'))
except Exception as e:
print(f"Error reading {filename}: {str(e)}")
if __name__ == "__main__":
read_deferred_mail()
Permission issues: Always use sudo
as queue files are owned by postfix user.
Binary appearance: The raw files contain metadata headers - use postcat
instead of direct file reading.
Message relocation: Simply copying to maildir won't work due to format differences.
To requeue all deferred messages for delivery attempt:
sudo postsuper -r ALL deferred
sudo postfix flush
For regular monitoring, consider these commands:
# Count deferred messages
sudo find /var/spool/postfix/deferred/ -type f | wc -l
# View deferred mail summary
sudo mailq | grep deferred