When working with remote IMAP servers where you only have standard IMAP access (no direct filesystem access), traditional tools like procmail become unusable. Many developers face this challenge when:
- Using multiple email clients across different devices
- Needing consistent filtering rules applied server-side
- Managing enterprise email accounts with restricted access
Here are the most effective tools for this specific use case:
1. imapfilter
A powerful Lua-based IMAP filtering tool that runs as a daemon. Sample configuration:
options.timeout = 120 options.subscribe = true -- Connect to server account = IMAP { server = 'imap.example.com', username = 'user@example.com', password = 'secret', ssl = 'tls1' } -- Move all GitHub notifications to Dev folder account.INBOX:contain_from('notifications@github.com'):move_to(account['Dev'])
2. Sieve Scripts with ManageSieve
If your server supports the Sieve filtering language (common with Dovecot/Cyrus):
require ["fileinto", "imap4flags"]; if address :contains "from" "support@" { fileinto "Support"; setflag "\\Seen"; }
3. Python with imaplib
For custom solutions, here's a Python script template:
import imaplib import email M = imaplib.IMAP4_SSL('imap.example.com') M.login('user', 'pass') M.select() typ, data = M.search(None, 'UNSEEN') for num in data[0].split(): typ, msg_data = M.fetch(num, '(RFC822)') msg = email.message_from_bytes(msg_data[0][1]) if 'urgent' in msg['Subject'].lower(): M.copy(num, 'Urgent') M.store(num, '+FLAGS', '\\Seen')
For production environments, consider:
- Running filters as cron jobs (for periodic execution)
- Using systemd services (for continuous monitoring)
- Implementing proper error handling and logging
- Adding OAuth2 support for modern authentication
This imapfilter rule handles complex X-Headers often used in enterprise systems:
account.INBOX:match_field('X-Ticket-ID', '^SR-[0-9]{5}'):move_to(account['Service Desk']) account.INBOX:match_field('List-ID', '.*security-announce.*'):move_to(account['Security'])
html
When working with IMAP-only mail servers, maintaining consistent message organization across multiple email clients becomes challenging. Traditional solutions like procmail work at the MTA level, but we need equivalent functionality for remote IMAP accounts.
An ideal solution should provide:
- Server-side rule execution (independent of email clients)
- Support for standard filtering actions (move, flag, delete)
- CRON-compatible scheduling
- Minimal dependencies
1. IMAPFilter (Linux)
A Lua-based configuration tool that executes rules directly against IMAP servers:
# Example imapfilter config
options.timeout = 60
options.subscribe = true
account = IMAP {
server = 'imap.example.com',
username = 'user',
password = 'pass',
ssl = 'tls1'
}
-- Move newsletters to folder
results = account.INBOX:contain_from('newsletter@')
results:move_messages(account['Newsletters'])
-- Mark high priority as read
account.INBOX:match_field('X-Priority', '1'):mark_read()
2. Sieve (Server-Side Solution)
If your IMAP server supports ManageSieve (Cyrus, Dovecot):
# Example Sieve script
require ["fileinto", "imap4flags"];
if header :contains "List-ID" "announcements" {
fileinto "Mailing Lists";
setflag "\\Seen";
}
if address :matches "From" "*@spamdomain.com" {
discard;
}
3. Python with IMAPClient
For custom filtering logic:
from imapclient import IMAPClient
with IMAPClient('imap.example.com', ssl=True) as server:
server.login('user', 'pass')
server.select_folder('INBOX')
# Find and process messages
messages = server.search(['UNSEEN', 'FROM', 'billing@'])
if messages:
server.move(messages, 'Processed/Bills')
server.add_flags(messages, [b'\\Seen'])
- Security: Store credentials securely (keyrings or encrypted configs)
- Performance: Process messages in batches for large mailboxes
- Error Handling: Implement proper connection recovery
For synchronizing filters across multiple IMAP accounts:
#!/bin/bash
# Sync processed emails between servers
imapfilter -c ~/.config/imapfilter/account1.lua
imapfilter -c ~/.config/imapfilter/account2.lua
rsync -az ~/.maildir/account1/processed/ ~/.maildir/account2/archive/
Implement logging and alerts:
# Sample log rotation config for imapfilter
/var/log/imapfilter.log {
weekly
missingok
rotate 12
compress
delaycompress
notifempty
}